Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: use 'local' modifier to hashref element

by nbtrap (Sexton)
on Jun 06, 2013 at 01:03 UTC ( [id://1037340]=note: print w/replies, xml ) Need Help??


in reply to Re^3: use 'local' modifier to hashref element
in thread use 'local' modifier to hashref element

Interestingly, the code:
#!/usr/bin/perl use warnings; use strict; my %hash; my @array; { local $hash{foo} = 'bar'; local @array; }
complains that you "Can't localize lexical variable @array", but it has no problem localizing $hash{foo}.

Even more interestingly, the code:

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %hash; our @array; { local $hash{foo} = 'bar'; local @array; print Dumper(\%main::); }
shows that the localization of the hash slot does _not_ manipulate the symbol table--even without "use strict".

Replies are listed 'Best First'.
Re^5: use 'local' modifier to hashref element
by shmem (Chancellor) on Jun 06, 2013 at 13:56 UTC
    ... complains that you "Can't localize lexical variable @array", but it has no problem localizing $hash{foo}.

    It has no problem localizing the value of an array element, either:

    my %hash; my @array; { local $hash{foo} = 'bar'; local $array[1] = 'foo'; print "inner scope:\n"; print "$_ => $hash{$_}\n" for keys %hash; print "[$_] $array[$_]\n" for 0..$#array; } print "outer scope:\n"; print "$_ => $hash{$_}\n" for keys %hash; print "[$_] $array[$_]\n" for 0..$#array; __END__ inner scope: foo => bar [0] [1] foo outer scope:

    Were you trying to localize the entire hash - as you did with @array - perl would complain also:

    Can't localize lexical variable %hash at foo line x.
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^5: use 'local' modifier to hashref element
by jakeease (Friar) on Jun 06, 2013 at 08:07 UTC

    Interestingly, the code: ... complains that you "Can't localize lexical variable @array", but it has no problem localizing $hash{foo}.

    Because $hash{foo} is a scalar and it is localizing the value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found