Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.

by ikegami (Patriarch)
on Dec 15, 2016 at 17:56 UTC ( [id://1177869]=note: print w/replies, xml ) Need Help??


in reply to Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.

Treat the expression giving the name of the variable as a reference.

So if the name is obtained by "specific_${var_name_modifier1}_var" and it's a scalar variable, you want

no strict qw( refs ); ${ "specific_${var_name_modifier1}_var" } = 123; say ${ "specific_${var_name_modifier1}_var" };

Replies are listed 'Best First'.
Re^2: Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.
by Anonymous Monk on Dec 15, 2016 at 18:37 UTC
    That is terrible code.

      Which is one of the reasons we repeatedly state not to use such code.

      Dominus says it best.

        > Which is one of the reasons

        Not really, there are many reasons, but that's not part of it! :)

        Ikegami's code is just a condensed one liner version of

        $string= "construct variable name"; say $$string;

        not much different from dereferencing syntax.

        On another note: Someone is trying to troll here ATM, better don't feed. :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

      Of course it is. That was already made extremely clear by the following:

      this is purely a curiosity and NOT for actual implementation anywhere....EVER. I know one should not do this i know what should NEVER EVER do this.....one should use hashes etc.... etc.....I know....

      So yeah, we shouldn't use symbolic references, which is why we ask perl to prevent us from using them by adding use strict; to our code. It's extremely clear that the OP knows this too, but they wanted to know how to use them anyway. Some low-level code (e.g. exporters, OO frameworks, namespace cleaners, etc) require using them, and its possible the OP is working on such code.

Log In?
Username:
Password:

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

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

    No recent polls found