in reply to How do create a variable in another package in Perl?
In addition to Mark Jason Dominus' paper discouraging using a name to create a variable, you can look at eval or just plain access the variable through its fully qualified name:
my $varname = "Foo::Bar::my_var"; { no strict 'refs'; my @array = @{ $varname } = (1,2,3); my %hash = %{ $varname } = (1 => 2); my $scalar = ${ $varname } = "Hello World"; } use Data::Dumper; eval <<'EOF'; print Dumper \@Foo::Bar::my_var; print Dumper \%Foo::Bar::my_var; print Dumper \$Foo::Bar::my_var; EOF
Most likely, you will use this technique for the wrong thing. It is very, very rarely needed to create variables in a package whose name you don't know yet. The one situation I know and accept is to set up @ISA for another package.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do create a variable in another package in Perl?
by Anonymous Monk on Dec 12, 2011 at 10:22 UTC |