Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to create dynamic global variables

by pankaj41483 (Initiate)
on Jul 01, 2010 at 13:21 UTC ( #847508=perlquestion: print w/ replies, xml ) Need Help??
pankaj41483 has asked for the wisdom of the Perl Monks concerning the following question:

I have a array variable that have name of modules that i needed.
I m geting these module names through reading array content in loop and calling the module methods.
Problem is i need to create global object variable for all the module name that are in array. array size is dynamic it could have a single module name or n number of module name. basicaly. basicaly the name of module that is in as array value is same as my moudle name.
how to create dynamic global variable that can individualy hold the module object. and from this objects i can call there other methods.
thanks,

Comment on How to create dynamic global variables
Re: How to create dynamic global variables
by kennethk (Monsignor) on Jul 01, 2010 at 14:39 UTC
    I apologize if this answer is off point; however I am having difficulty discerning your spec from the above post.

    By default, all ordinary Perl arrays that are currently in scope (i.e. have not been garbage collected) are globally visible and accessible through a fully-qualified name. However, assuming you are a good citizen and are using strict, you need to declare an array you wish to be globally visible using our. You can then interact with this array as would normally expect.

    #!/usr/bin/perl use strict; use warnings; package Mine; our @array = qw(1 2 3); package main; print join "x", @Mine::array;

    If this does not meet your spec, post a more specific question with examples of code as per How do I post a question effectively?.

Re: How to create dynamic global variables
by linuxer (Deacon) on Jul 01, 2010 at 14:44 UTC

    I have problems to completely understand your request; I hope my guess will be helpful to you.

    You could use a global hash which uses the module's name as key and the object as value.

    #! /opt/perl/bin/perl # vim: set ts=4 sw=4 sts=4 et nu: use strict; use warnings; use Data::Dumper; my %object; my @modules = qw( CGI ); for my $mod ( @modules ) { eval "use $mod; \$object{$mod} = $mod->new; 1;" or die "eval faile +d: $@\n"; } print Dumper \%object;
Re: How to create dynamic global variables
by Anonymous Monk on Jul 01, 2010 at 17:35 UTC
Re: How to create dynamic global variables
by metaperl (Curate) on Jul 01, 2010 at 19:40 UTC
    I have a array variable that have name of modules that i needed. I m geting these module names through reading array content in loop and calling the module methods.
    Please post the source code for what you said above, be sure to use CODE tags to wrap it.
    Problem is i need to create global object variable for all the module name that are in array.
    It sounds like you mean that you need to access that list of modules in numerous places in your code, and so you want get at that array from many places. Well, a package variable is "global" and accessible from other modules. But the question becomes, why are you placing the modules that you need in an array variable?
    array size is dynamic it could have a single module name or n number of module name. basicaly. basicaly the name of module that is in as array value is same as my moudle name. how to create dynamic global variable that can individualy hold the module object. and from this objects i can call there other methods. thanks,
    You are doing things in a very unorthodox way. There are plenty of CPAN modules for this type of thing, but I think you might want to read some good books on Perl programming. But anyway, here is how to do what you want:
    package X; use vars qw(@modules); @modules = qw(A B C); 1; package U; use X; warn $_ for @X::modules; 1;
    but you really should express your overall goal and get help on working out the dynamic loading in a more "standard" way :)



    The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

    -- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-12-29 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls