fletcher_the_dog has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have some code that caused me some annoying problems recently that was something like this:
use strict; my $i = 45; my $x; my $end = 30; for $i (0..67) { # do some useful stuff last if $i == $end; } print $i."\n";
I would expect that this would print out "30" in the end, but instead it prints out "45". It appears that some implicit localization is going on with "$i". I knew this happened with "$_" but is this supposed to happen with all variables? Is this documented anywhere?

Replies are listed 'Best First'.
•Re: "for" surprise
by merlyn (Sage) on Nov 21, 2003 at 19:51 UTC
    Even though you spell it "f-o-r" in your code, it's still pronounced "foreach", so let's see what perldoc perlsyn says:
    The "foreach" loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword "my", then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with "my", it uses that variable instead of the global one, but it's still localized to the loop. This implicit localisation occurs *only* in a "foreach" loop.
    And I know we talk about it in the llama book.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Well, I had looked in the perldoc but I guess I was looking in the wrong place. Even so, I think this localization is very unDWIMy. Is there anyone out there who has been bitten by it? Is there anyway to get a warning about it? (I already tried -w and "use warnings")
        Perhaps you forget that Perl is not DWIM, but DWLM (Do What Larry Means).

        Luckily, Larry has given us notes about what he wants Perl to do. {grin}

        Seriously, the localization is an important positive property of foreach loops, and something that while you may have just learned, has been a property of foreach loops since Perl version 1.

        Asking for a warning on the locallization there would be like getting a warning on an assignment operator because "the value of the variable has now changed!". No, not gonna happen. Sometimes, you have to think for yourself, and learn what there is to learn about the language.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        Relying on the fact that the loop iterator has a defined value after leaving the loop is bad style in many languages, as many languages explicitly state the value of the loop iterator as undefined.

        Personally, I prefer to create loop iterators that are scoped only to the loop block, as that completely avoids the issue:

        for my $i (1..67) { ... };

        That way, $i can't be used outside the loop, and I consider that a good thing. But why are you using a loop over a fixed range instead of a loop from 1 to $end anyway?

        perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: "for" surprise
by Roger (Parson) on Nov 22, 2003 at 00:48 UTC
    The for loop behaves differently depending on how the variable $i is declared. Consider the following two programs -
    #!/usr/local/bin/perl -w #!/usr/local/bin/perl -w use strict; use strict; my $i = 45; our $i = 45; for $i (0..67) { for $i (0..67) { last if $i == 10; last if $i == 10; investigate(); investigate(); } } print $i."\n"; print $i."\n"; sub investigate { sub investigate { print "\$i=$i\n"; print "\$i=$i\n"; } }
    The difference is that the left program declares a my variable, while the right program declares an our variable.

    However, when I run the programs, I get completely different behaviour inside the for loop:

    45 0 45 1 45 2 45 3 45 4 45 5 45 6 45 7 45 8 45 9 $i=45 $i=45
    Note that inside the for loop, the value of the top level my $i variable is not affected, while the global our $i variable gets temporarily affected. An interesting observation is made: Perl localizes the $i variable differently inside the for loop, depending on how $i is declared in the first place.

    When the top level $i variable is declared as a my variable, the for loop is equivalent to -
    my $i = 45; for (0..67) { my $i = $_; ... }
    When the top level $i variable is declared as an our variable, the for loop is equivalent to -
    our $i = 45; for (0..67) { local $i = $_; ...
Re: "for" surprise
by Cody Pendant (Prior) on Nov 21, 2003 at 22:02 UTC

    Can you let us know what it was you were trying to do with this code or similar?

    I get the impression there's something else going on, because as presented here, the code is pretty illogical. Why make $i 45, then make it 0 two lines later?



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      One of the cases, when I use similar constructs is a linear search on an array, you stop, when you found an entry. You scan the entries of an array one after the other, jump out of the loop, when you found something. After the loop, you examine the index. If it is lower than the endvalue of the loop, you found something at the index:

      my $i; for ($i=0; $i<=$#list; $i++) { last if found_it( $list[$i] ); } print "Found at position $i\n" if ($i <= $#list);
      (Of course, code is simplified and not tested)

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

        why not move everything to a function and let the function return the index where the data is found?
        sub search{ my ($item,$end) = @_; my $i; my $found = 1; foreach $i (0..$end) { if (some condition on the list){ return ($found, $i); } } return (0,0); }

        since perl supports multiple return values, we could return $found as well if someone wants it.

Re: "for" surprise
by melora (Scribe) on Nov 22, 2003 at 18:17 UTC
    I've got to agree with Corion, this is questionable style. I would suggest setting a different variable to mark where (or why or how) the loop was exited, if that's needed after the loop exits. Besides, to be fair to the iterator variable, its only job was to count through the loop, not to provide information after the loop is over.