confused a little by use strict

Discussion in 'Perl Misc' started by dan, Dec 21, 2008.

  1. dan

    dan Guest

    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?
     
    dan, Dec 21, 2008
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Dec 21, 2008
    #2
    1. Advertising

  3. dan

    Peter Scott Guest

    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/
     
    Peter Scott, Dec 22, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    2
    Views:
    545
    Daniel
    Nov 5, 2003
  2. Amy Snyder

    A little confused...

    Amy Snyder, Apr 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    335
    Kevin Spencer
    Apr 15, 2004
  3. Xiaoshen Li

    question-- a little confused

    Xiaoshen Li, Dec 14, 2005, in forum: Java
    Replies:
    4
    Views:
    2,780
    Chris Smith
    Dec 15, 2005
  4. Replies:
    8
    Views:
    382
    P.Hill
    Feb 14, 2006
  5. shapper
    Replies:
    0
    Views:
    292
    shapper
    Mar 11, 2007
Loading...

Share This Page