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

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

  • Comment on Re^2: use 'local' modifier to hashref element

Replies are listed 'Best First'.
Re^3: use 'local' modifier to hashref element
by kennethk (Abbot) on Jun 05, 2013 at 15:49 UTC
    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".

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

        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.

Re^3: use 'local' modifier to hashref element
by rovf (Priest) on Jun 05, 2013 at 15:44 UTC
    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.

        I tried - and to surprise, indeed, it worked!!!

        So once more I learned something new....
        -- 
        Ronald Fischer <ynnor@mm.st>