Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Getting a variable name from a reference

by erikharrison (Deacon)
on Mar 05, 2002 at 13:31 UTC ( [id://149359]=note: print w/replies, xml ) Need Help??


in reply to Re: Getting a variable name from a reference
in thread Getting a variable name from a reference

You're right - it can't be done without saving the name of the name of the variable and some additional jiggery pokery. At least it can't be done with a hard reference like this. The reason it can't be done is because a hard reference is just and address - so the "thingie" that the address points to might have no varable name, such as an anonymous hash:

$hashref = {'Anonymous' => 'Hash'}; #hash has no varable #name - just reference

Additionally, a varable might have more than one name, if it has aliases.

Back in Perl 4 (which I've never used) there were no hard refs, so you had to use variable - name - plus eval tricks to do reference type stuff. This might do what you want it to (warning - untested code):

#usr/bin/perl -w use strict; my %hash1 = ('A' => 1); my $hashname1 = 'hash1'; my %hash2 = ('B' => 2); my $hashname2 = 'hash2'; eval qq (print qq(Hashname: $$hashname1 Values:); print join ",",sort +keys %{$hasname1})" ); eval qq (print qq(Hashname: $$hashname1 Values:); print join ",",sort +keys %{$hasname1}) );

But these kinds of eval tricks I'm not used to - and you probably shouldn't use them anyway. Also, you can't print out a memory address here, unless you add in a pair of hard refs.

Cheers,
Erik

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found