In Perl, eval can do similar things, but symbolic references are “better” (I’m fairly sure it’s where PHP got the idea, and the syntax, from.) e.g.
$foo = "bar";
$$foo = "potatoes"; # $$foo = access the variable named in $foo, i.e. $barprint$bar; # prints potatoes
Reading other responses, it seems like Python’s globals object is not entirely dissimilar, especially if you know how Perl deals with symbolic references under the hood.
But just because you can doesn’t mean you should. If you use strict; in Perl, it will fail to compile most of this nonsense. Use a hash / associative array / dictionary / whatever your language (natural and/or programming) calls them instead.
And I’m pretty sure that even without strict, local variables can’t be accessed at all the symbolic way, which is probably for the best. (NB: local is a subtle thing in Perl. By “local” here, I mean the so-called my variables that aren’t accessible outside their scope. local variables are actually localised globals. Enjoy that thought.)
In Perl,
eval
can do similar things, but symbolic references are “better” (I’m fairly sure it’s where PHP got the idea, and the syntax, from.) e.g.$foo = "bar"; $$foo = "potatoes"; # $$foo = access the variable named in $foo, i.e. $bar print $bar; # prints potatoes
Reading other responses, it seems like Python’s
globals
object is not entirely dissimilar, especially if you know how Perl deals with symbolic references under the hood.But just because you can doesn’t mean you should. If you
use strict;
in Perl, it will fail to compile most of this nonsense. Use a hash / associative array / dictionary / whatever your language (natural and/or programming) calls them instead.And I’m pretty sure that even without
strict
, local variables can’t be accessed at all the symbolic way, which is probably for the best. (NB:local
is a subtle thing in Perl. By “local” here, I mean the so-calledmy
variables that aren’t accessible outside their scope.local
variables are actually localised globals. Enjoy that thought.)