Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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";

Replies are listed 'Best First'.
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.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588965]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-19 22:35 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (115 votes). Check out past polls.