Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.


---
demerphq

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


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2020-02-18 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (79 votes). Check out past polls.

    Notices?