http://www.perlmonks.org?node_id=307998


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.


---
demerphq

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