Steve Grazzini wrote:
>
> On Dec 13, 2003, at 9:07 AM, Rob Dixon wrote:
> > As a final thought, I would point out that $_ is a package ('our')
> > variable, but is localised by 'map', 'grep', 'foreach (LIST)' and
> > 'while (<>)'.
>
> 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 $_.

> 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 :)

> The exception to this is when an our() declaration (which is a
> lexically scoped symbol, remember) hides the special global.
>
>      % perl -le '{ package X; our $INC = 1 } print "[$INC]"'
>      []

Yes, because this is the same as

  perl -le "$X::INC = 1; print qq/[$INC]/"

I wonder if you, or anybody, knowswhere the localisation of $_
is documented? My Camel was 'borrowed' and feeling rather lonely.

Thanks,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to