Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Localizing hash keys in an AoH, continued

by kennethk (Abbot)
on Jul 22, 2013 at 18:09 UTC ( #1045697=perlquestion: print w/replies, xml ) Need Help??
kennethk has asked for the wisdom of the Perl Monks concerning the following question:

This is a follow up to Localizing hash keys in an AoH. tobyink's suggestion of guards has worked well, until I discovered I'd naively used the code in a case where I actually needed to delete the hash key/value pair when scope exited, instead of just restoring a value. I cobbled together the code
sub localised_hashvalue { package localised::hashvalue; my ($ref, $key, $value) = @_; my $exist = exists $ref->{$key}; my $old = $ref->{$key}; $ref->{$key} = $value; return bless [$ref, $key, $exist, $old]; sub DESTROY { my ($ref, $key, $exist, $old) = @{+shift}; $ref->{$key} = $old; delete $ref->{$key} if !$exist; } }
which performs the necessary function, albeit in a clunkier-feeling way. The target hashrefs in the array are cross-referenced in a couple ways, so I really do need to localize the value in the hashref (as opposed to localizing an array slice and inserting duplicate hashrefs). Any ideas? Obviously this is more academic, since I have functioning code.

#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: Localizing hash keys in an AoH, continued
by rjt (Deacon) on Jul 22, 2013 at 19:12 UTC

    I suppose it depends what you mean by "clunkier way". Your sub is reasonably compact, although the usage becomes somewhat cumbersome. Here's an alternative that creates a tie'd hash allowing arbitrary localized manipulation (without the need for actual scope, though you still can, of course). The original hash is not affected by changes to the tied hash, but the tied hash will be affected by changes made to the original one, in case you need to affect some data globally. The semantics of all of this can be changed pretty easily.

    #!/usr/bin/env perl use 5.012; use warnings; use Data::Dump qw/dump/; my %hash = ( some => 'original', keys => 'for reference', ); tie my %local, 'RJT::LocalHash', \%hash; $local{foo} = 'bar'; delete $local{some}; $local{keys} =~ y/e/E/; # Still uses FETCH $hash{both} = 'Operations on original hash affect both'; dump \%local; dump \%hash;


    { # tied RJT::LocalHash both => "Operations on original hash affect both", foo => "bar", keys => "for rEfErEncE", } { both => "Operations on original hash affect both", keys => "for reference", some => "original", }

    RJT::LocalHash source

      Thanks for the response. The information flow seems off from my needs based upon how I'm looking at your code, but somehow it shook loose the idea of performing the localization on the full hash, and not trying to compound it with setting keys. That gave me this:
      sub localised { package localised; use Scalar::Util 'reftype'; my $ref = shift; my $reftype = reftype $ref or die "$ref is not a reference"; my @old = $reftype eq 'SCALAR' ? $$ref : $reftype eq 'ARRAY' ? @$ref : $reftype eq 'HASH' ? %$ref : die sprintf "Unsupported reftype %s", reftype $ref; return bless [$ref, @old]; sub DESTROY { my ($ref, @old) = @{+shift}; my $reftype = reftype $ref; $reftype eq 'SCALAR' ? ($$ref) : $reftype eq 'ARRAY' ? @$ref : %$ref = @old; } }

      I feel like there may be a reasonable way to roll optional assignment in there simultaneously, but cleanly and across data types is escaping me.

      I've never been a big user of tie, so thank you for giving something to chew on. I can vaguely see how a full understanding of how to implement this in tie combined with guards would result in a really swanky solution, but my brain is too stupid today to implement that one.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045697]
Front-paged by rjt
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-06-22 04:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.