use Lexical::Alias; my ($s, %h); alias $h{key}, $s; print \$s, "\n", \$h{key}, "\n"; => SCALAR(0x811e2f4) SCALAR(0x811e2f4)
Don't use $a and $b for variable names. They have special meanings.
Update(s): To make it clear once and for all: the code above makes the lexical scalar variable $s an alias for $h{key}. The syntax is alias <src>, <dst>. This confuses many people, because it's different from common functions like push @ary, ... or index $str, ... etc. where object comes first. Here's a paragraph from the POD:
Version 0.04 introduced the $Lexical::Alias::SWAP variable. When it is true, the arguments to the aliasing functions are expected in reverse order; that is, the alias comes first, and the source variable second.(Thanks to Jenda from perlmonks.org for requesting this.)
I don't recommend using SWAP though, it induces more unmaintainability and confusion.
In reply to Re: Make hash key an alias
by calin
in thread Make hash key an alias
by pope
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |