Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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;

Output:

{ # 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

package RJT::LocalHash { use parent 'Tie::Hash'; use List::MoreUtils qw/uniq/; use Carp; sub TIEHASH { my ($class, $orig) = @_; croak 'Expected HASH ref, not `'.ref($orig)."'" unless 'HASH' eq ref $orig; bless { orig => $orig, del => { }, new => { } }, $class; } sub STORE { delete $_[0]{del}{$_[1]}; $_[0]{new}{$_[1]} = $_[2] + } sub EXISTS { not exists $_[0]{del}{$_[1]} and (exists $_[0]{new}{$_[1]} or exists $_[0]{orig}{$_[1] +}) } sub FETCH { return if exists $_[0]{del}{$_[1]}; exists $_[0]{new}{$_[1]} ? $_[0]{new}{$_[1]} : $_[0]{orig}{$_[1]} } sub FIRSTKEY { # Initialize the iterator as union of both hash key sets # minus anything that's been locally deleted my @each = grep { not exists $_[0]{del}{$_} } uniq keys $_[0]{orig}, keys $_[0]{new}; $_[0]{each} = \@each; shift @each; } sub NEXTKEY { shift $_[0]{each} } sub DELETE { $_[0]{del}{$_[1]} = 1; $_[0]{new}{$_[1]} // $_[0]{orig}{$_[1]}; } }

Edit: Minor fix to EXISTS.


In reply to Re: Localizing hash keys in an AoH, continued by rjt
in thread Localizing hash keys in an AoH, continued by kennethk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2014-11-26 03:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (162 votes), past polls