in reply to Re^2: Creating strings with specified names
in thread Creating strings with specified names
Writing Perl that creates variables with a name specified at run time is pretty easy, but it's generally not a good idea.
$name = 'foo'; $$name = 'bar'; print "foo = $foo\n"; __END__ foo = bar
Note that doesn't work if you use strict (which is always a good idea). In that case, you wind up with something like this:
my $name = 'foo'; { no strict 'refs'; $$name = 'bar'; } print "foo = $main::foo\n";
See how I still had to use the explicit package name to refer to the variable for printing.
You can do a lot with eval too.
|
|---|