Phoenix.pm: Getting a name of a variable.
Rob.Svirskas at motorola.com
Thu Sep 13 17:06:41 CDT 2001
In general, (well, in "main") this should work:
my %some_hash = (a => 1, b => 2);
my $name = *some_hash;
...optionally, you can strip off the module name:
my $name = (split(/::/,*some_hash))[-1];
Of course, if you want to do this within a subroutine (which it sounds like the case), it gets hairy:
no strict 'refs';
my $name = shift;
my $var = join '::',(caller),$name;
%some_hash = (a => 1, b => 2);
...of course, your %some_hash can't be declared with 'my', otherwise it won't be in the symbol table. This means that you can't be using strict 'vars' there.
To be honest, Bryan's suggestion of
may not look particularly elegant, but it will work. The only hairball here would be printing the name of the variable that was passed in, and not the subroutine variable...
From: erik.tank at bpxinternet.com [mailto:erik.tank at bpxinternet.com]
Sent: Thursday, September 13, 2001 1:41 PM
To: phoenix-pm-list at happyfunball.pm.org
Subject: Phoenix.pm: Getting a name of a variable.
I have a debugging module that I have been working on. One of the methods
prints out the contents of a hash that is passed to it. I think that it
would be cool to have the name of the hash appear above the output. Does
anyone know how to take a variable an get it's name?
erik.tank at bpxinternet.com
More information about the Phoenix-pm