Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > confused a little by use strict

Reply
Thread Tools

confused a little by use strict

 
 
dan
Guest
Posts: n/a
 
      12-21-2008
Howdy,

The code:

use strict;

my $foo = 0;

for $foo (0,1) {
print "$foo\n";
}

print "$foo\n";

outputs

0
1
0

So $foo comes out of the loop unchanged. OK, if thats how it works, then
why complain if I write

use strict;

my $foo = 0;

for $mistake (0,1) {
print "$foo\n";
}

print "$foo\n";

which outputs
Global symbol "$mistake" requires explicit package name ...?

Since it complains, this implies that in the first case, $foo would come
out of the loop with value 1, doesn't it?


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-21-2008
dan wrote:
> The code:
>
> use strict;
> my $foo = 0;
> for $foo (0,1) {
> print "$foo\n";
> }
> print "$foo\n";
>
> outputs
>
> 0
> 1
> 0
>
> So $foo comes out of the loop unchanged.


The docs refer to it as "implicit localisation". Please read about
foreach loops in "perldoc perlsyn".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Peter Scott
Guest
Posts: n/a
 
      12-22-2008
On Sun, 21 Dec 2008 16:58:21 +0000, dan wrote:
> use strict;
>
> my $foo = 0;
>
> for $foo (0,1) {
> print "$foo\n";
> }
>
> print "$foo\n";
>
> outputs
>
> 0
> 1
> 0
>
> So $foo comes out of the loop unchanged. OK, if thats how it works, then
> why complain if I write
>
> use strict;
>
> my $foo = 0;
>
> for $mistake (0,1) {
> print "$foo\n";
> }
>
> print "$foo\n";
>
> which outputs
> Global symbol "$mistake" requires explicit package name ...?
>
> Since it complains, this implies that in the first case, $foo would come
> out of the loop with value 1, doesn't it?


Your contention appears to be that since perl uses a 'temporary' variable
for the foreach iterator, shouldn't 'use strict' give it a pass if it's
not declared? The foreach semantics have evolved over the years and the
counterclaims to your argument are not as strong as they once were, but
nevertheless, at this point, perl elects to prefer to be consistent with
the "fully specify (or 'use vars') all package variables when using
strict" rule. Notice that if we turn strict off, we can see that perl
uses the package variable for the iterator if there's no lexical of the
same name (rather thab creating a new lexical):

$::foo = 'package';
for $foo ( 42 ) {
print "Package = $::foo\n";
}

# Package = 42

Just always say "for my ..." and you won't be bothered by these issues.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
How to use this class? A little bit confused. Thank You. shapper ASP .Net 0 03-11-2007 04:54 PM
How can I use switch -s and 'use strict' at the same time Ting Wang Perl Misc 5 10-06-2005 02:03 PM
why is important to use : use strict? Martina Perl Misc 6 09-20-2005 12:14 AM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM



Advertisments