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

exporting vars

by b888 (Beadle)
on Nov 08, 2004 at 11:24 UTC ( [id://406027]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings all.

I'm trying to export some variables from module. While using filename same as package all seems ok.

file 'A.pm'

package A; use strict; BEGIN { use Exporter(); use vars qw(@EXPORT @ISA); @EXPORT = qw($var1); @ISA = qw(Exporter); } use vars @EXPORT; $var1 = "1";
file 'B.pl'
#!/usr/bin/perl package B; use strict; use A; print "$var1\n";

But when i'm trying to use package name that differs form filename, strict raises an error.

file 'A1.pm'

package A; ...

file 'B.pl'

... use A1; ...
Is this perl problem, or i'm doing something wrong?

Replies are listed 'Best First'.
Re: exporting vars
by PodMaster (Abbot) on Nov 08, 2004 at 11:59 UTC
    Exporter exportes when import is called, and import is called when you use `perldoc -f use', `perldoc -f import', Tutorials.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: exporting vars
by Thelonius (Priest) on Nov 08, 2004 at 12:39 UTC
    This will work, if you really need to do this:
    BEGIN {require A1; import A;}
Re: exporting vars
by fergal (Chaplain) on Nov 08, 2004 at 13:36 UTC
    When you do use A;, that includes a call to A->import but for A1 there is no import method so no variables are exported. One solution is to give A1 an import method - one that just redirects to A's import method.
    package A1; sub import { A->export_to_level(1, @_); }
    export_to_level is a special routine provided by Exporter that allows you to control where the symbols will be exported to. The 1 here means export 1 level up. So it's saying "Hey A, don't export your symbols into my package (A1) export them into the package I was called from (B in your example)".

    A quick plug for one of my modules. Exporter::Easy lets you write

    BEGIN { use Exporter(); use vars qw(@EXPORT @ISA); @EXPORT = qw($var1); @ISA = qw(Exporter); } use vars @EXPORT; $var1 = "1";
    as
    use Exporter::Easiest q( EXPORT => $var1 ); $var1 = "1";
    it handles setting up all the other Exporter arrays too if you want it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found