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

Re^2: Localizing hash keys in an AoH, continued

by kennethk (Monsignor)
on Jul 22, 2013 at 21:04 UTC ( #1045724=note: print w/ replies, xml ) Need Help??


in reply to Re: Localizing hash keys in an AoH, continued
in thread Localizing hash keys in an AoH, continued

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.


Comment on Re^2: Localizing hash keys in an AoH, continued
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-10-02 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (61 votes), past polls