Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Getting a variable name from a reference

by Sweeper (Pilgrim)
on Mar 05, 2002 at 21:57 UTC ( [id://149519]=note: print w/replies, xml ) Need Help??


in reply to Getting a variable name from a reference

Do not confuse variables and values. References apply to values, so you can reference anonymous arrays, anonymous hashes and anonymous subroutines. In these cases your request cannot be fulfilled.

On the other hand, with typeglob assignment, several variables can be associated to a single value. In this case, you would have several variable names for the single value your reference points to.

Further, you can reference an element of an array

$r_array_elem = \$array[1]
instead of the usual
$r_array = \@array
which references the whole array. In this case, what would your name-retrieving operation yield?

Note this code was taken verbatim from page 20 of Advanced Perl Programming (Sriram Srinivasan, O'Reilly). The first chapter deals with references, and is very interesting. The only glitch is that it does not warn sufficiently about the banana skins you get with symbolic variables. Nevertheless, this is a good book.

Replies are listed 'Best First'.
Re: Re: Getting a variable name from a reference
by Fool on the Hill (Acolyte) on Mar 06, 2002 at 10:36 UTC
    Hello,

    Thanks for the clarification. I am going to borrow a colleagues Advanced Perl Programming and read some more (I foolishly thought I understood references).

    I still think it would be nice (but maybe impossible?) to find out what refers to a particular piece of data/value. I understand that there is a reference count that means that even if a lexical goes out of scope if it has been referenced and the reference is still in scope then the value will be preserved.

    After reading your reply and Programming Perl again, I now see that the reference is basically a one way link between reference and the value (referent). The diagram 8.1 (from Programming Perl) is particularly useful. It seems like this is one case where symbolic references are the ones to use. (It has been hammered into me that soft references are bad.....)

    Many Thanks for everybodys help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found