Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Make hash key an alias

by pope (Friar)
on Nov 18, 2003 at 06:26 UTC ( #307921=perlquestion: print w/replies, xml ) Need Help??

pope has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
$a = 2; *b = \$a; # $b is an alias for $a
Now I have a package variable %w, and I want to make $w{foo} an alias for $a. How to do that?

Replies are listed 'Best First'.
Re: Make hash key an alias
by chromatic (Archbishop) on Nov 18, 2003 at 06:31 UTC

    Tie a hash. Override STORE. Update $a by reference.

    See perltie for details.

Re: Make hash key an alias
by ysth (Canon) on Nov 18, 2003 at 07:03 UTC
    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.
Re: Make hash key an alias
by demerphq (Chancellor) on Nov 18, 2003 at 15:26 UTC
    #!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


Re: Make hash key an alias
by TomDLux (Vicar) on Nov 18, 2003 at 14:21 UTC
    $w{foo} = \$a; ${$w{foo}} = 17; print $a; __OUTPUT__ 17

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      $w{foo} is a reference, not an alias.
Re: Make hash key an alias
by calin (Deacon) on Nov 18, 2003 at 15:05 UTC
    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.

      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


        Well, the way I read this its actually wrong. 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.

        Re-read my post. Clearly stated the other way around. The OP decides if that suits him.

        You're right that Array::RefElem is the best solution. I didn't know about that module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://307921]
Approved by davido
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2020-01-17 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?