Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Using variable contents in new variable name

by imp (Priest)
on Dec 11, 2006 at 02:07 UTC ( #588965=note: print w/ replies, xml ) Need Help??


in reply to Using variable contents in new variable name

Perl allows this through symbolic references... but their use results in messy code that is difficult to maintain, and violates strict (which should always be used).

You should use a different type of data structure instead - arrays and hashes are very appropriate here.

my @work_array; $work_array[5] = "plumbing"; my %work_hash; $work_hash{5} = "plumbing";


Comment on Re: Using variable contents in new variable name
Download Code
Re^2: Using variable contents in new variable name
by geekphilosopher (Friar) on Dec 11, 2006 at 06:20 UTC

    If it turns out that you actually need symbolic references, and still want to use strict, you can put a "no strict refs" just before the symbolic reference work, which will temporarily disable strictness for symbolic references. Remember to re-enable them, though!

      Remember to re-enable them, though!

      Do it only in the scope of a small code block

      use strict; ... # strict in effect here ... { no strict q{refs}; # strict refs not in effect in the scope # of this code block so do something with # symbolic refs here ... } ... # strict refs back in effect again without having # to remember to re-enable it. ...

      But, as imp says, don't go there if you can possibly help it.

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-04-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls