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:
- Why it's stupid to `use a variable as a variable name'
- A More Direct Explanation of the Problem
- 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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.