Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How do create a variable in another package in Perl?

by Corion (Patriarch)
on Dec 12, 2011 at 09:55 UTC ( [id://943052]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://943052]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found