<?xml version="1.0" encoding="windows-1252"?>
<node id="343572" title="Re: variable with $$" created="2004-04-08 06:56:33" updated="2005-08-11 07:32:32">
<type id="11">
note</type>
<author id="190859">
bart</author>
<data>
<field name="doctext">
You're asking for symbolic references, huh? At least, I think you are. Well: don't do it, unless you know &lt;em&gt;really well&lt;/em&gt; what you're doing &amp;mdash; 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 &lt;em&gt;very&lt;/em&gt; dangerous. You never know whether he'll input the name of a variable you've used yourself in your program.
&lt;P&gt;See [dominus]' classic 3 articles on his website, for more arguments against it:
&lt;ol&gt;
&lt;li&gt;[http://perl.plover.com/varvarname.html|Why it's stupid to `use a variable as a variable name']
&lt;li&gt;[http://perl.plover.com/varvarname2.html|A More Direct Explanation of the Problem]
&lt;li&gt;[http://perl.plover.com/varvarname3.html|What if I'm Really Careful?]
&lt;/ol&gt;
&lt;P&gt;In general: don't do it. Use a hash. Like this:
&lt;code&gt;
my %v;   # my hash for user variables
$v{Sam}++; 
# or:
my $name = 'Sam';
$v{$name}++;
&lt;/code&gt;
&lt;P&gt;Oh, and if you &lt;em&gt;really&lt;/em&gt; want to do it, try &lt;code&gt;$$name&lt;/code&gt; or &lt;code&gt;${$name}&lt;/code&gt;, which both do the same thing, and only work on global (= package) variables &amp;mdash; and only with &lt;code&gt;no strict 'refs'&lt;/code&gt;. Just to show you it can be done...  ;-)
&lt;P&gt;If all you want is to work on predefined variables with no input from the user/outside world, try hard references instead.
&lt;code&gt;
my $var = \$sam;
$$var++;  # increments $sam
&lt;/code&gt;
Yes, the syntax is exactly the same. That's one reason why coding without &lt;tt&gt;strict 'refs'&lt;/tt&gt; in place, is a rather bad idea.</field>
<field name="root_node">
343493</field>
<field name="parent_node">
343493</field>
</data>
</node>
