MarsRover has asked for the wisdom of the Perl Monks concerning the following question:

Before people trip out... let me start by saying 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.... but nevertheless, i know it is possible and to expand my horizons into the realm of the full madness-possibilities of perl, (one must know how the dark or f'd up side of the force works in order to fully understand the force....etc...) I'd like to know how one would THEORETICALLY do the following:

again, this is purely a scholarly exercise.....

Say i have a variable with a value in it.

I now have another variable, defined and assigned. This second variable's name contains, as A PART of it, the full value contained in the first variables name.

Say i want to print the value contained within the second variable by referencing the variable's name using the first variable's name?

Code always speaks louder than words.... this code doesn't work....:

my $var_name_modifier1 = '100'; my $var_name_modifier2 = '200'; my $specific_200_var = "this is from 200\n\n"; my $specific_100_var = "this is from 100\n\n"; print "\n\n\n\n"; print 'this is what we have for 100: '; print $specific_{$var_name_modifier1}_var; print " \n\n";

if it helps, here is the error.....

Bareword found where operator expected at ./test.pl line 19, near "}_v +ar" (Missing operator before _var?) syntax error at ./test.pl line 19, near "}_var" Execution of ./test.pl aborted due to compilation errors.

hopefully this is obvious enough to you all what exercise in perl knowledge i'm trying to access here.

thank you in advanced for containing the angle's side of your perl-brain and allowing the devil's side to speak today

sincerely, MR
  • Comment on Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.
by kennethk (Abbot) on Dec 15, 2016 at 17:46 UTC
    The relevant documentation is Symbolic references in perlref. One important thing to understand is that you cannot interact with lexical variables using symbolic references -- one reason your code won't work is because you can't see your set of specific variables. Correcting your example code,
    use strict; use warnings; my $var_name_modifier1 = '100'; my $var_name_modifier2 = '200'; our $specific_200_var = "this is from 200\n\n"; our $specific_100_var = "this is from 100\n\n"; print "\n\n\n\n"; print 'this is what we have for 100: '; NO_STRICT_BLOCK: { no strict "refs"; print ${"specific_${var_name_modifier1}_var"}; } print " \n\n";
    Note the (likely) unexpected behavior seen for:
    use strict; use warnings; our $value = "Valuable information"; print 'this is what we have: '; NO_STRICT_BLOCK: { my $value = 'Ha ha, I changed it!'; no strict "refs"; print ${"value"}; }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.
by LanX (Saint) on Dec 15, 2016 at 17:24 UTC
    Well you already made clear that it's normally a stupid idea, and here the foot gun:

    C:>perl $a=1; $b1c=42; print ${"b${a}c"}; ^D 42

    Nota Bene no strict 'refs' needed

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

    Update corrected vars to refs

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

    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" };
      That is terrible code.

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

        Dominus says it best.

        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.

Re: 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 17:52 UTC
    Don't you think this is better? :)
    perl -le'$s=sub{sprintf"this is from %d\n\n",shift};print $s->($_) for + 100,200,300'
Re: Contain yourselves please: THEORETICAL question about variable values being used as A PART of another variable's name.
by Anonymous Monk on Dec 16, 2016 at 01:30 UTC

    Before people trip out... let me start by saying 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.... but nevertheless, i know it is possible and to expand my horizons into the realm of the full madness-possibilities of perl, (one must know how the dark or f'd up side of the force works in order to fully understand the force....etc...) I'd like to know how one would THEORETICALLY do the following:

    How do you know this? Who told you? Where did you read about it?

    Did you know about this?

    site:perlmonks.org variable variable name -> Variable Variables -> http://perl.plover.com/varvarname.html

    Now you know :p