Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

by kennethk (Abbot)
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045724]
[choroba]: erix :-D

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-05-22 16:26 GMT
Find Nodes?
    Voting Booth?