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

If I define a variable:
my $foo;
how might I get the variable's name as a string?
$varname = ????($foo);
where $varname becomes 'foo' so I can write
$hash{????($foo)} = $foo;
instead of
$hash{'foo'} = $foo;
Many thanks to tachyon and sgifford (as well as all who replied). Their questions focused my fuzzed brain and their code suggestion solved my problem, if not exactly in the way I imagined, certainly in a way that works well and taught me something.

Isn't that what monks are for?

Replies are listed 'Best First'.
Re: getting the name of a variable to use as string
by tilly (Archbishop) on Sep 17, 2004 at 01:27 UTC
    You can't.

    The internal reason is that the exact same scalar may have multiple names by which it can be accessed at the same time. For instance in the following code the exact same variable is $foo and $bar:

    my $foo = "example"; foreach my $bar ($foo) { # Right now $foo and $bar are the same }
    There is no general way to know which one you'd want back, so Perl doesn't even think of trying to answer that question.

    Instead you'll need to step back and think about the problem that you're trying to solve and then solve it in a different way. My feeling is that you're working too hard to avoid a simple line of code. But if you really feel strongly about it, you can achieve the effect with something like this:

    foreach my $var_name (qw(foo bar baz)) { $hash{$var_name} = eval "\$$var_name"; }
    but that would probably be a bad idea.

    UPDATE: Thinking about it I had the awful idea that someone actually might be able to write a function that stares at the optree for the caller and figures out what the passed in variable was called (in simple cases). This would be an even worse idea than the eval in my books.

      I'm really trying to avoid heavily repetitious code of the form
      $hash{'foo1'} = $foo1; $hash{'foo2'} = $foo2; ... $hash{'foon'} = $foon;
      where foo1..foon are column names of a thoroughly denormalized relational table each row of which I want to put into a hash of hashes. Looks like a job for a code generator ;-) Thanks all for your help!

        Your thinking appears totally left field. First with DBI you can retrireve rows as hashrefs where the key is already the column name. Next how does the data get into $foo1 in the first place. Why not just put it into a hash in the first instance and then use the hash?

        cheers

        tachyon

        First, as tachyon says, I'm quite curious how you've gotten this information into all these variables, and why you didn't just put them into a hash to begin with. But assuming that's done by somebody else in code you don't control...

        You're approaching the problem backwards. Instead of taking a variable and getting the name out, take the name and turn it into a variable, with a symbolic reference. Something like this should work, though I haven't tested it:

        foreach my $i (qw(foo1 foo2 foo3 ... foon)) { $hash{$i} = $$i; }
Re: getting the name of a variable to use as string
by sintadil (Pilgrim) on Sep 17, 2004 at 01:21 UTC

    What you want is a symbolic reference. For most purposes, they're not needed in Perl. You can often accomplish what you want from symbolic references with other means. Perhaps you can specify why you think you need one?

      A symbolic reference will not accomplish what he's looking for. The symbolic reference mechanism only accesses package globals. The variable that he's going to wind up accessing dynamically is a lexical.

      I agree that the problem "smells wrong" though, and the need should probably be rethought.

        A symbolic reference will not accomplish what he's looking for. The symbolic reference mechanism only accesses package globals. The variable that he's going to wind up accessing dynamically is a lexical.

        sintadil RTFMs. I see. Thanks for the clarification.