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

Greetings Monks,

I need to create a function that returns the value of a variable based on name. The goal is to be able to pass it a parameter and have it match a variable named that, and return its contents. The following code works fine, however uses global variables. Every time I make the variables private, the code ceases to work, and I REALLY don't want to use globals. Could someone show me an elegant way to do this? Also, when I use strict it will not compile. Thanks a lot for your time =)

Best Regards!

smack
sub foo { my $parameter = $_[0]; print "\nParameter: $parameter\n"; $ipaddress = "foo_0"; $username = "foo_1"; $password ="foo_2"; $sessionid = "foo_3"; $version = "foo_4"; $returning = ${$parameter}; return $returning; } my $returned = &foo(@ARGV[0]); print "Returned: $returned\n";

Replies are listed 'Best First'.
Re: Creating a new variable named the contents of another
by Happy-the-monk (Canon) on Sep 27, 2004 at 12:28 UTC

    Consider using a simple  %hash   with the names as hash keys:

    Setting the values:
      $hash{  name } = "value"; # or
      $hash{ $name } = $value;

    Returning a single value from a subroutine:
      return $hash{ $name };

    Saves you trouble.
    Cheers, Sören

    Edit: "Returning a..." and some more comments.

Re: Creating a new variable named the contents of another
by ccn (Vicar) on Sep 27, 2004 at 12:29 UTC

    May be a hash would be better

    my %h = ( ipaddress => "foo_0", username => "foo_1", password => "foo_2", sessionid => "foo_3", version => "foo_4", ); sub foo { my $parameter = $_[0]; print "\nParameter: $parameter\n"; die "Bad parameter $paramenter" unless exists $h{$parameter}; return $h{$parameter}; } my $returned = &foo(@ARGV[0]); print "Returned: $returned\n";
Re: Creating a new variable named the contents of another
by dragonchild (Archbishop) on Sep 27, 2004 at 12:33 UTC
    You're grasping for a hash. You could use soft references for that, but you shouldn't.

    The reason it won't work with lexicals is because they're not in the symbol table, while globals are. (If you don't fully understand that, you need to read more from the Camel book.)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Creating a new variable named the contents of another
by NetWallah (Canon) on Sep 27, 2004 at 12:30 UTC
    Is there a reason you are not using a hash for this ?

    #Untested.. my %foo = ( IP => "2.3.4.5" ,USER => "Someone" ,PASSWORD => "Bad Idea" , .. etc ... ); my $returned = $foo{$ARGV[0]}; print .. etc ..

        Earth first! (We'll rob the other planets later)

Re: Creating a new variable named the contents of another
by smack (Beadle) on Sep 27, 2004 at 13:03 UTC
    Thank you all for your replies.... I can see a lot of scenarios now where I should have used a hash. Seems I have a bit of code I will be rewriting. The examples you guys gave me helped me a lot and inspired me to do more with hashes(As it seems I should have been doing).

    Best Regards,

    smack