Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: variable with $$ by bart
in thread variable with $$ by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-28 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found