Re: perl syntax oddity
Yitzchak Scott-Thoennes <[email protected]>
| Newsgroups | gmane.comp.lang.perl.fun |
|---|---|
| Organization | bs"d |
| Message-ID | <[email protected]> |
On Thu, Sep 18, 2003 at 12:07:16PM +0300, Ilmari Karonen <[email protected]> wrote: > I guess it's because for() localizes the variable it's iterating over, > and needs the glob for that. Oddly enough, however, local() does not > accept a glob that way: for $x () does a more complete localization than local($x) does: use Tie::Scalar; tie $x, "Tie::StdScalar"; sub checktie { print '$x is ', (tied($x)?'':'not '), "tied in $_[0]\n" } for $x (1) { checktie('for') } { local $x; checktie('local($x)') } { local *x; checktie('local(*x)') } > sub getaglob {*foo} > $foo = "pong\n"; > for ${&getaglob} ("ping ") { print $foo } > print $foo; > > works, printing "ping pong", while: > > sub getaglob {*foo} > $foo = "pong\n"; > { local ${&getaglob} = "ping "; print $foo } > print $foo; > > fails with "Can't localize through a reference"! This error seems to be removed in 5.8.1 and it works there (though the error entry was erroneously left in perldiag). But this prints "pingping": sub foo:lvalue { $foo } $foo = " pong\n"; { local &foo; print $foo="ping" } print $foo;