Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do create a variable in another package in Perl?

by PerlOnTheWay (Scribe)
on Dec 12, 2011 at 08:41 UTC ( #943038=perlquestion: print w/ replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

Specifically,what's the right way to create a scalar/array/hash in another package in Perl?

Thank you very much

Comment on How do create a variable in another package in Perl?
Download Code
Re: How do create a variable in another package in Perl?
by moritz (Cardinal) on Dec 12, 2011 at 08:46 UTC

    There's no real need to "create" variables in another packages, because they are entries in the symbol table that can autovivify. Nonetheless you can do something like @YourPackge::VariableName = (1, 2, 3);

    Though it is rare that this is a useful and a good idea. Maybe try to explain what you want to achieve in the end, then we can think about better approaches. It could be an XY Problem.

    (For example it might be better to export variables from a module that you use in the namespace in question.)

      This is needed when the VariableName and the value of the variable(here (1,2,3)) are both determined on the fly

        In which case it isn't needed. Use a hash instead, or maybe some other construct entirely. Show us sample code where you think such a thing is needed and we'll show you a much better way to do it.

        True laziness is hard work

        Hello, I have small joke for you :D

        <GoodHumor>

        I agree with moritz :) there is a strong suggestion of an interface design problem, a strong suggestion for a need to just pass args

        Here is my complete analysis

        • +1 key phrase "in another package"
        • +1 newbee key phrase "in Perl"
        • +1 asking for the syntax
        • +1 while posessing the key keyword (package) to find the correct syntax
        • +1 key phrase "is needed"
        • +1 key phrase "on the fly"
Re: How do create a variable in another package in Perl?
by Anonymous Monk on Dec 12, 2011 at 09:07 UTC
Re: How do create a variable in another package in Perl?
by Corion (Pope) on Dec 12, 2011 at 09:55 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://943038]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-09-24 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (243 votes), past polls