Steve Grazzini wrote:Actually, $_ isn't localized by 'while(<>)':
% echo test | perl -le 'for ("const") { print while <> }' Modification of a read-only value attempted at -e line 1.
Which occasionally jumps up and bites people.
Thanks for that Steve. I guess if you think about it then, since it's equivalent to the awful
while (defined($_ = <>)) { : }
it's actually not a loop control variable at all, but an explicit assignment to $_.
That's right. On the other hand, since the implicit assignment only happens when readline() is the condition of a while() loop or statement modifier, I'm not sure why it couldn't be equivalent to:
while (defined(local $_ = <>)) { ... }
Or, following the example of foreach():
{ local $_; # sort of while (defined($_ = <>) { ... } }
And for the trivia buffs: another interesting thing about the special package variables (it's the globs/symbols that are actually special, see below) is that they're always in package "main".
% perl -le '{ package X; $inc++ } print "[$inc]"' [] % perl -le '{ package X; $INC++ } print "[$INC]"' 1
Yes, but what I find most surprising is that $INC is $main::INC even though @INC and %INC are special variables but $INC isn't :)
Yeah -- the forcing-into-main:: applies to the whole glob (the symbol table entry) and not just the system variable itself. The same odd thing applies to $STDIN, %ARGV, etc.
*INC is especially noteworthy, though, because in order to
push @INC, $object;
$object needs to have an INC() method, and the symbol-table lookup in a subroutine definition *also* forces *INC into package main::.
package Foo; sub INC { # this is main::INC ! }
Which has probably irritated somebody somewhere.
-- Steve (*raising his own hand*)
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>