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

Re: Make hash key an alias

by calin (Deacon)
on Nov 18, 2003 at 15:05 UTC ( #307992=note: print w/replies, xml ) Need Help??


in reply to Make hash key an alias

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.

Replies are listed 'Best First'.
Re: Re: Make hash key an alias
by demerphq (Chancellor) on Nov 18, 2003 at 15:38 UTC

    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.

        Ahem. Caugh. Yes. It seems the meaning of those words didnt penetrate my thick skull. Apologies.


        ---
        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://307992]
help
Chatterbox?
and the web crawler heard nothing...

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










    Results (113 votes). Check out past polls.

    Notices?