Re: same variable defined twice

[email protected] ("Alan C")
Newsgroups perl.beginners
Message-ID <[email protected]>
On Thu, May 22, 2008 at 5:57 AM, Octavian Rasnita <[email protected]>
wrote:

> Hi,
>
> I've tried the following script and it works fine:
>
> use strict;
> my $text = 1;
> my $text = 2;
> print $text;
>
> Shouldn't perl disallow defining the $text variable a second time in the
> same script if using "use strict"?


Probably not, as already has been said.

And, as said, the use of: use warnings; *will* warn you.

Next shows a block or lexical scope (mask does not happen) versus when in
the same scope (in global, not using a separate scope block) where mask can
happen.

#!/usr/bin/perl
use strict;
use warnings;
my $text = 1;
# begin block next
{my $text = 2;
print "In block we have ",$text,"\n";}
# block ended on previous line
# so, next is outside of block
print "1st Outside block we have ",$text,"\n";
my $text = 3;
print "2nd Outside block we have ",$text,"\n";
# end

-- 
Alan.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.