Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: use 'local' modifier to hashref element

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


in reply to use 'local' modifier to hashref element

Yes, it works.
use strict; use warnings; use Data::Dumper; my $hash_ref = {1 => 2, 3 => 4}; BLOCK: { local $hash_ref->{1} = 5; print Dumper $hash_ref; } print Dumper $hash_ref;
The clearest documentation on this, IMHO, is Temporary Values via local() in perlsub. Essentially, you can localize any hash or array value, which will be restored once you exit the current scope. This trick is frequently used with the %SIG handlers. When you do the localization, you are localizing the element in the hash that the reference points at.

Update: When to Still Use local() is actually the better doc link for this question.


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

Replies are listed 'Best First'.
Re^2: use 'local' modifier to hashref element
by schetchik (Beadle) on Jun 05, 2013 at 15:35 UTC
    I forgot to write in my main message. I'm mostly interesting in localization of variables with lixical scope. Because
    A local just gives temporary values to global (meaning package) variables. It does not create a local variable ęperldoc
    but my variable is not global( or package ) it's just hash reference.

    That's why I have some doubts

      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.

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

      I'm mostly interesting in localization of variables with lixical scope.
      As you already point out, local acts on the symbol table of the respective package, i.e. on global variables. In your case, I don't see a way how you can have a single hash element automatically restored at the end of the block.

      If this feature is really important to you, did you consider a package global variable declared with "our" (i.e. a lexically scoped global variable) as an alternative? However, I personally would restore the hash entries manually at the end of the block.

      -- 
      Ronald Fischer <ynnor@mm.st>
        You may want to reconsider what you've said here.
        use strict; use warnings; use Data::Dumper; my %hash = (1 => 2, 3 => 4); BLOCK: { local $hash{1} = 5; print Dumper \%hash; } print Dumper \%hash;
        What you've said is true for localizing lexical scalars; however localizing values in lexical arrays and hashes is no problem. See When to Still Use local(), item #3. Sorry if I've misread your intent.

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

        Test it. It works. It is more reliable than manually restoring code because you don't have redundant code to mess up on, and because you don't have to worry about exceptions skipping that code. I've been using the trick since Perl 5.005. The ONLY type of scalar that you can't use local on is one defined with my. And that was rejected not because it was hard - it is easy - but because Larry Wall thought that it would confuse people to allow that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1037249]
help
Chatterbox?
LanX YT auto-caption: "The Uk sends a breakfast negotiator to brothels"
[tye]: cool. For some reason I couldn't remember it nor come up with a good search strategy (other than crowd sourcing).
[davido]: hm, that seems to also survive $< manipulations @corion so good call.
[Corion]: tye: I also more randomly stumbled over it, as "who am i" unix implementation didn't really provide much help. But Wikipedia linked me to the List of Unix Programs (or something like it), and from there ...
[talexb]: Wasn't Brexit the name of the first Santana album?
[tye]: That's funny, saying getlogin() is less secure. For some uses, it is far superior to getpwuid(), precisely for reasons of security.
[LanX]: search strategy => Perl Functions by Category
[Corion]: ... I found discussion of the who utility and that it uses the POSIX 2008 getlogin function (but that function was available much earlier and thus even exists in Perl, as a search for getlogin on CPAN brought up Perl 5.26 as first hit
[tye]: oh, LanX, but I was thinking that it was not a function that Perl provided.
[Corion]: Yeah, I also went a more roundabout way, just to find that the solution had been with Perl all along! ;)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-06-23 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (554 votes). Check out past polls.