| [reply] [d/l] [select] |
You can only directly do it the other way: *a = \$w{foo}. See Alias for other ideas. At least one module looks like it provides a general tie mechanism for faking aliases. | [reply] [d/l] |
#!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
| [reply] [d/l] [select] |
| [reply] [d/l] |
$w{foo} is a reference, not an alias.
| [reply] [d/l] |
You can do it the other way around with Lexical::Alias for lexicals (only the scalar variable needs
to be lexical). This doesn't use tie.
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. | [reply] [d/l] [select] |
Well, the way I read this its actually wrong. (UPDATE: and calin said as much, although in a way that may not be immediately apparent to readers unfamiliar with Lexical::Alias, so I still feel this node is worthy, if only to expand on what is happening here.)
In this you arent making $h{key} an alias to $s. You are making $s be an alias to $h{key}. Not the same thing. Use Array::RefElem and you dont have this problem, and can do it with either lexicals or globals. (Making the composite type contain an alias to an arbitrary Scalar that is.)
use Lexical::Alias qw(alias_r);
my ($s, %h);
print '$s:',\$s, "\n", '$h{key}:',\$h{key}, "\n";
print "---\n";
alias_r \$h{key}, \$s;
print '$s:',\$s, "\n", '$h{key}:',\$h{key}, "\n";
__END__
$s:SCALAR(0x1ac2430)
$h{key}:SCALAR(0x1acefd4)
---
$s:SCALAR(0x1acefd4)
$h{key}:SCALAR(0x1acefd4)
Come to think of it IMO its ridiculous all these different modules with weirdo names having alias related tools in them. I think this calls for a consolodation release with them all in one place... (broquaint are you listening?)
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [d/l] [select] |
| [reply] |