Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

variable with $$

by Anonymous Monk
on Apr 08, 2004 at 04:27 UTC ( #343493=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks , I have a variable like that :
my $name = Sam;
what I wanna do is create a variable from reading $name which will look like this :
my $newvar = $Sam;
so I want to print the value for name with a $ in the front of it. I tried :
my $newvar = $\"$name"; or my $newvar = '$'"$name";
no luck ,, any idea? can someone help? thanks

Replies are listed 'Best First'.
Re: variable with $$
by bart (Canon) on Apr 08, 2004 at 10:56 UTC
    You're asking for symbolic references, huh? At least, I think you are. Well: don't do it, unless you know really well what you're doing — and basically, if you have to ask how to do it, I dare to bet that you don't. Making a name based on a user's input is always very dangerous. You never know whether he'll input the name of a variable you've used yourself in your program.

    See dominus' classic 3 articles on his website, for more arguments against it:

    1. Why it's stupid to `use a variable as a variable name'
    2. A More Direct Explanation of the Problem
    3. What if I'm Really Careful?

    In general: don't do it. Use a hash. Like this:

    my %v; # my hash for user variables $v{Sam}++; # or: my $name = 'Sam'; $v{$name}++;

    Oh, and if you really want to do it, try $$name or ${$name}, which both do the same thing, and only work on global (= package) variables — and only with no strict 'refs'. Just to show you it can be done... ;-)

    If all you want is to work on predefined variables with no input from the user/outside world, try hard references instead.

    my $var = \$sam; $$var++; # increments $sam
    Yes, the syntax is exactly the same. That's one reason why coding without strict 'refs' in place, is a rather bad idea.
Re: variable with $$
by The Mad Hatter (Priest) on Apr 08, 2004 at 04:52 UTC
    Put $name in double quotes and then backslash (escape) the extra $...
    my $name = "Sam"; my $newvar = "\$$name"; # or even ... my $newvar = '$' . $name; print $name, "\n", $newvar;
    which prints
    Sam $Sam
Re: variable with $$
by kvale (Monsignor) on Apr 08, 2004 at 04:56 UTC
    You can do this with
    my $name = "Sam"; my $newvar = '$' . $name;


      thanks all
Re: variable with $$
by zentara (Archbishop) on Apr 08, 2004 at 13:24 UTC
    Yep, it is far from my lowly status to say that Bart is right, but he is. Whenever you start thinking about using a "variable in a variable" just stop!!...and think how can I do this with a hash? It will save you tons of trouble, and once you get the "hang of hashes", you'll love them.

    I'm not really a human, but I play one on earth. flash japh
Re: variable with $$
by Steve_p (Priest) on Apr 08, 2004 at 04:54 UTC


    my $newvar = "\$" . "$name";
Re: variable with $$
by asarih (Hermit) on Apr 08, 2004 at 04:51 UTC
    $var="test"; $test="fred"; print ${$var};

    update Note to self: read thoroughly before post.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://343493]
Approved by graff
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-05-27 11:25 GMT
Find Nodes?
    Voting Booth?