How can I use a variable as a module name in Perl?
Posted
by mjn12
on Stack Overflow
See other posts from Stack Overflow
or by mjn12
Published on 2010-04-15T19:52:02Z
Indexed on
2010/04/16
3:53 UTC
Read the original article
Hit count: 301
I know it is possible to use a variable as a variable name for package variables in Perl. I would like to use the contents of a variable as a module name. For instance:
package Foo;
our @names =("blah1", "blah2");
1;
And in another file I want to be able be able to set the contents of a scalar to "foo" and then access the names array in Foo
through that scalar.
my $packageName = "Foo";
Essentially I want to do something along the lines of:
@{$packageName}::names; #This obviously doesn't work.
I know I can use
my $names = eval '$'. $packageName . "::names"
But only if Foo::names
is a scalar. Is there another way to do this without the eval
statement?
© Stack Overflow or respective owner