in reply to "for" surprise

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.

Replies are listed 'Best First'.
Re: •Re: "for" surprise
by fletcher_the_dog (Friar) on Nov 21, 2003 at 20:03 UTC
    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
        That way, $i can't be used outside the loop, and I consider that a goo +d thing.
        I agree that makes sense and I do it myself all the time, but sometimes it might not be what you want to do and it might not be good thing in which case even if you think you are changing the behavior, you are not.
        But why are you using a loop over a fixed range instead of a loop from + 1 to $end anyway?
        It was a small example not real code. Where it says "# do some useful stuff" there would be ... some useful stuff :-)