Phoenix.pm: Getting a name of a variable.

Svirskas Rob-ERS007 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;
print $name,"\n";

...optionally, you can strip off the module name:
my $name = (split(/::/,*some_hash))[-1];
print $name,"\n";


Of course, if you want to do this within a subroutine (which it sounds like the case), it gets hairy:

sub printStuff
{
no strict 'refs';
  my $name = shift;
  my $var = join '::',(caller)[0],$name;
  print $name,"\n";
  print %$var,"\n";
}

%some_hash = (a => 1, b => 2);
&printStuff('some_hash');


...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
 print "var",%var;
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...

                                       - Rob

-----Original Message-----
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?

Thanks,

Erik Tank
602-817-4705
erik.tank at bpxinternet.com



More information about the Phoenix-pm mailing list