in reply to Local for lexicals

Have I missed something??

$ perl -wMstrict -e 'my $e = 77; warn $e; { my $e = 22; warn $e }; warn $e' gives, when run, :

77 at -e line 1. 22 at -e line 1. 77 at -e line 1.
As, I think, expected.

Update:

Corrected error - original code would never produce stated results coz of missing intialization.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Local for lexicals
by JadeNB (Chaplain) on Aug 10, 2009 at 15:35 UTC
    The problem is that the inner my $e is a completely different variable to the outer one, so that functions closing over the outer one don't ‘see’ it. To illustrate:
    perl -wMstrict -e 'my $e = 77; my $f = sub { warn $e }; $f->(); { my $ +e = 22; $f->() } $f->();'
    gives
    77 at -e line 1 77 at -e line 1 77 at -e line 1