Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Make hash key an alias

by demerphq (Chancellor)
on Nov 18, 2003 at 15:26 UTC ( #307998=note: print w/replies, xml ) Need Help??

in reply to Make hash key an alias

#!perl -l use Array::RefElem qw(hv_store); my %w; my $x='foo'; hv_store(%w,'bar',$x); $w{bar}='baz'; print $w{bar}; print $x; $x='bop'; print $w{bar}; print $x; __END__ baz baz bop bop

Incidentally the difference between Array::RefElem and Lexical::Alias is that the later only does aliases between like typed items in the current pad (ie, it can make %foo an alias of %bar but it cant make $foo{$bar} an alias of $bar{bar} without making every other key/value an alias as well). Array::RefElem cant make a lexical an alias of another, but it can make the SV part of a composite object an alias to another. So in a sense the two modules complement each other nicely. Also its worth reading the docs on Lexical::Alias, it doesnt work correct under 5.6.1 and doesnt raise an error when it fails. So its essentially not safe to use pre 5.8 afaict.

And your wording is a touch ambiguous. If your question was asking how you can aliase the key part only to a variable, the answer is simple: you cant. Hash keys arent real scalars so they can't be aliased. Hash values are a different matter.


    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-10-30 01:53 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (277 votes). Check out past polls.