Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Local for lexicals

by Bloodnok (Vicar)
on Aug 10, 2009 at 15:32 UTC ( [id://787358]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://787358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2024-04-19 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found