Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Importing dynamic variables

by Anonymous Monk
on Nov 16, 2006 at 23:25 UTC ( [id://584633]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to be able to export a set of variables that I create on the fly, but am having trouble working out the syntax. Below is an example of the kind of thing I want to do
package gen_var; use base 'Exporter'; my @exported_symbols; my @variables = qw(one two three); my $counter = 1; our @EXPORT_OK; foreach my $variable (@variables) { eval "our \$$variable = $counter; push \@exported_symbols, \$$varia +ble"; $counter++; } my $export_vars = join(', ', @exported_symbols); eval "\@EXPORT_OK = ($export_vars)"; 1;
then in another piece of code:
use gen_var($one); print $one;
I realise some purists may bristle at this kind of code, but now that I've had a crack at writing it, I'm interested in finding out how to get it to work

Replies are listed 'Best First'.
Re: Importing dynamic variables
by davido (Cardinal) on Nov 16, 2006 at 23:29 UTC

    This whole idea makes me want to throw my hands in the air and run out of the room screaming as though my hair were on fire.

    One problem you have is in the "use" statement: It should not be "use gen_var($one);, but rather, use gen_var qw( one );.

    As long as you're doing this, wouldn't it be easier to directly manipulate the symbol table as a hash?


    Dave

      This whole idea makes me want to throw my hands in the air and run out of the room screaming as though my hair were on fire
      I knew that would be the response when I posted! Still, I'm curious to know how to get this to work.
      It should not be "use gen_var($one);", but rather, "use gen_var qw( one );"
      But wouldn't that import the subroutine "one" and not the variable "$one"?
      As long as you're doing this, wouldn't it be easier to directly manipulate the symbol table as a hash?
      I did try that accessing the symbol table. Say each variable was an object and not a scalar
      package gen_var; use banana; my @variables = qw(one two three); my $counter = 1; foreach my $variable (@variables) { no strict; *$variable = banana->new($counter++) } 1;
      and from the classing package:
      use gen_var; $one->get_counter();
      That doesn't work, but I'm not sure how to fix it...
        Ah,
        package gen_var; use banana; my @variables = qw(one two three); my $counter = 1; foreach my $variable (@variables) { no strict; my $ns_var = caller() .'::'.$variable; *$ns_var = \banana->new($counter++); } 1;
        should work
Re: Importing dynamic variables
by gellyfish (Monsignor) on Nov 17, 2006 at 10:24 UTC

    Firstly you will need to have all of that in a BEGIN block in order for @EXPORT_OK to have the values in it when import is called by the use gen_var.

    Also I would eschew the second eval and simply do something likeL

    BEGIN { foreach my $variable (@variables) { eval "our \$$variable = $counter; push \@exported_symbols, \ +$$variable"; $counter++; push @EXPORT_OK, "\$$variable"; } }
    I do something similar in Sys::Utmp for what it's worth. Actually it's worse because there I export the symbols into one module that then exports them to the callers namespace :-O

    /J\

      thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-18 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found