Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by kennethk (Monsignor)
on Jun 05, 2013 at 15:49 UTC ( #1037256=note: print w/ replies, xml ) Need Help??


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

local acts on the value, not the variable. Don't think about it as localizing the hash reference, nor even localizing the hash. If you look at the code I've provided, I have no explicit package variables, and I'm localizing a value stored in an anonymous hash.

The reason for your cited line from the documentation is to try to prevent people from thinking that my and local do anything even remotely similar. (That's a little hyperbole, which I can expound upon as necessary).

Update: Code is worth a thousand words.

use strict; use warnings; use Data::Dumper; my $hash_ref = {1 => 2, 3 => 4}; BLOCK: { local $hash_ref->{1} = 5; $hash_ref->{3} = 6; print Dumper $hash_ref; } print Dumper $hash_ref;
Note the localized value associated with the key 1 is restored when you exit the block, but the (unlocalized) value associated with the key 3 is not.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re^3: use 'local' modifier to hashref element
Select or Download Code
Re^4: use 'local' modifier to hashref element
by nbtrap (Sexton) on Jun 06, 2013 at 01:03 UTC
    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".

      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.

      ... 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'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-08-22 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls