4

I am pretty sure this works with perl but I dont know how to code it. I can imagine it with eval, but that is not I am looking for.

my $foo = 0;
my $varname = "foo";


$($varname) = 1;  # how to do this? 
# I want to access a scalar that name is in a other scalar
# so $foo should be 1 now.

Thanks

2
  • 4
    Why isn't the other scalar stored in a hash in the first place?
    – Quentin
    Jun 8, 2011 at 21:49
  • 2
    You're looking for symbolic references
    – Mat
    Jun 8, 2011 at 21:53

4 Answers 4

8

What you're trying to do is called a symbolic reference, and the syntax is ${$varname} (or just $$varname for simple cases). But this is almost always a bad idea, because it tends to produce extremely hard to diagnose bugs. That is why it is prohibited by use strict.

You can say no strict 'refs' to allow symbolic references, but you really, really, shouldn't.

The two main alternatives to symbolic references are hashes and hard references. It's hard to say which one is better suited to your situation, because you haven't really explained what you're trying to do.

0
3

$$varname = 1 does what you want, but this is prohibited when use strict; is in effect, and thus it is considered bad style.

4
  • hi I had this in mind but with use strict I get an problem with scalar refence and without it is not working. my $vn = "foo"; my $foo = 2; $$vn = 33; print $foo; // prints out 2 and not 33, ???
    – chris01
    Jun 8, 2011 at 21:53
  • Err, $$varname is a deref of a scalar ref and $varname is set as a scalar, not a ref to scalar
    – mrk
    Jun 8, 2011 at 21:54
  • right, so how can I do what I described in my entry post (without eval)=
    – chris01
    Jun 8, 2011 at 21:57
  • 3
    Suggested edit: This is considered bad style, and thus it is prohibited when use strict is in effect.
    – mob
    Jun 8, 2011 at 22:00
2

Perl has two separate but largely compatible variable systems.

Package variables, which are either fully qualified names $Some::Package::variable or lexical names declared with our. Package variables live in the symbol table, are global to the whole program, can be the target of a symbolic dereference, and can be given a dynamic scope with local.

Lexical variables, declared with my, comprise the other variable system. These variables do not live in a package or symbol table (instead they live in a lexical pad which is attached to a scope). These variables are not global, can not be symbolically referenced, and can not have dynamic scope. This is why you can not use $$varname and expect it to find a lexical variable.

You have a few ways to deal with this issue:

  • use package variables, either fully qualified names, or declared with our, keep strict off, and use symbolic references:

    our $x = 1;
    our $y = 'x';
    say $x;  # 1
    $$y = 5; # this line is an error if `use strict` is loaded
    say $x;  # 5
    
  • use package variables and walk the symbol table:

    $main::x = 1;
    my $y = 'x';
    
    ${$main::{$y}} = 5;  # ok with `use strict`
    say $main::x;  # 5
    
  • the best practices way is to use a hash (which is what the above two examples are doing behind the scenes, since the symbol table itself is a hash)

    my %data = (x => 1);
    my $y = 'x';
    $data{$y} = 5;
    say $data{x};  # 5
    

The danger with symbolic references is that it is often far too easy to turn your program into spaghetti code, or to overwrite variables that you did not intend to. By using an explicit hash, you limit the magic of what you are doing to a well defined and limited scope. The hash itself can be lexical, allowing proper automatic garbage collection of your variables.

0

The Perl FAQ answers the specific original question:

http://learn.perl.org/faq/perlfaq7.html#How-can-I-use-a-variable-as-a-variable-name-

It includes warnings, examples, and alternatives in great detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.