in reply to Re^4: Indirect variable name
in thread Indirect variable name
sorry I don't understand this argumentation, if you want to make a symderef on a lexical you can always use eval
Not if a closure didn't include that variable:
use strict; use warnings; my $sub = do { my $x = 4; my $y = 5; sub { my $a = shift; print 'Accessing $x: ', $x + $a, "\n"; print 'Accessing $y from eval: ', eval '$y + $a', "\n"; }; }; $sub->(2); __END__ Accessing $x: 6 Use of uninitialized value in addition (+) at (eval 1) line 1. Accessing $y from eval: 2
Whoops. Perl knows at compile time which lexicals in outer scopes are needed for a closure, and only stores those.
With the current current behaviour only eval can be used to detect that. That's OK because eval has the "evil, don't use" stigma on it.
If symbolic deref would also look in lexicals, every deref might break this optimization.
I hope it's a bit clearer now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Indirect variable name
by LanX (Saint) on Nov 21, 2008 at 10:01 UTC | |
by moritz (Cardinal) on Nov 21, 2008 at 10:35 UTC | |
by LanX (Saint) on Nov 21, 2008 at 10:58 UTC | |
by LanX (Saint) on Nov 21, 2008 at 11:20 UTC |