bowei_99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I want to print the name of a variable to STDOUT, not the contents. For example, if my code is

use strict; use warnings; use Data::Dumper; my %hash_name = (one => "blah");
I want to print '%hash_name', not 'one' or 'blah'. The context of this is that I have an array of hashes of hashes, and I'd rather not add an additional level of complexity just for this. It's also one more thing to manage (and that could get messed up) if I were to do that. For now, I figure name of the variable should be fine; later, I can write a lookup hash of the variable name and description.

How can I do this?

-- Burvil

Replies are listed 'Best First'.
Re: Print name of variable to STDOUT?
by ikegami (Patriarch) on Aug 08, 2006 at 21:54 UTC

    Data::Dumper::Simple Be sure to read the Bugs and Limitations sections. It probably should not be used in production.

    Update: Oh! For just the name, there's PadWalker. You're probably doing something you really really really shouldn't if you need PadWalker.

Re: Print name of variable to STDOUT?
by GrandFather (Saint) on Aug 08, 2006 at 22:04 UTC

    Is the "additional level of complexity just for this" just for diagnostic purposes, or is it something sneeky that the "user" ends up seeing?

    If it is for diagnostic purposes you might want to take a look at the debugging support in Perl rather than poluting your code with diagnostic lint that obfusicates intent and probably adds bugs of its own.

    For a very brief overview of the debugger see Using the Perl Debugger in the Tutorials section.


    DWIM is Perl's answer to Gödel
      It's for both debugging and to clarify the output. Since this will go into a report, I'll need a lookup hash mapping the variable names with userfriendly descriptions, but initially, just the variable names should do.

      I thought it might help if I posted my code, to show the context. Here it is:

      sub GetStats { my $ref_files = shift @_; my (%lkup_by_who, %lkup_by_how, %lkup_by_from, %lkup_by_ftp); foreach my $file (@$ref_files) { open "LAST", "<", $file or croak "Cannot open file $file - $!\n"; print "Reading file $file ... \n"; while (<LAST>) { my ($who, $how, $from, @junk) = split (/\s+/); next if (!$who or !$how or !$from); next if (/wtmp begins/); if ($how eq "console") { $how = $from; next; } $from =~ s/:\d+(\.\d+)?//g; $lkup_by_who{$file}{"$who"}++; $lkup_by_from{$file}{"$from"}++; if ($how eq "ftp") { $lkup_by_ftp{$file}{"$who"}++; } } close (LAST); } return (\%lkup_by_who, \%lkup_by_from, \%lkup_by_ftp); }
      As you can see, it returns an array of hash of hashes. To avoid unnecessary complexity, I want to avoid putting it in one big hash, with keys 'who', 'from', and 'ftp'. Can I tell perl to output the variable names %lkup_by_who, \%lkup_by_from, \%lkup_by_ftp? I know that these variables are only defined within the subroutine, and so I'd have to put it in one big hash or print the information within the subroutine for it work.

      -- Burvil

        You could use a hash to hold onto the hashes with very little extra work:

        sub GetStats { my $ref_files = shift @_; my %lkup_by = (who => {}, how => {}, from => {}, ftp => {}); foreach my $file (@$ref_files) { ... while (<LAST>) { ... $lkup_by{who}{$file}{$who}++; $lkup_by{from}{$file}{$from}++; if ($how eq "ftp") { $lkup_by{ftp}{$file}{$who}++; } } close (LAST); } return ($lkup_by{who}, $lkup_by{from}, $lkup_by{ftp}); }

        I don't see where you need to get at the names however. Note too that you don't need to interpolate variables into strings, just use them directly.


        DWIM is Perl's answer to Gödel
Re: Print name of variable to STDOUT?
by starbolin (Hermit) on Aug 10, 2006 at 06:22 UTC

    Do you mean "Print all the variables I've defined in my subroutine" or "Print all the variables I'm returning from my subroutine" or some other group of variables?

    You either need to know the name of some thing or the index to the thing ( in very general meaning of name and index ) in order to manipulate a thing. If you know the name of it just print the name. If you know the index of it then dereference to get the name. If you don't know the name ahead of time and you don't have an index to it; then you can't reference it. Simple.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}