Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Export/import variables

by Anonymous Monk
on Mar 18, 2003 at 02:44 UTC ( [id://243859]=perlquestion: print w/replies, xml ) Need Help??

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

I couldn't find an answer to this one anywhere . . .

package Bio; $VERSION = '0.01'; require Exporter; use vars qw/@ISA/; @ISA = qw(Exporter); @EXPORT = qw(%users); sub import { my $self = shift; my %args = @_; $path = $args{path}; } print "hello" if $path; 1; __END__
How do I get the module to print hello?

Replies are listed 'Best First'.
Re: Export/import variables
by pfaut (Priest) on Mar 18, 2003 at 03:03 UTC

    It looks like you're expecting $path to get set by the import routine and then be seen by the body of the package but the code is executing the other way around.

    'use Module' can be looked at as a combination of 'require Module' and 'Module::import()'. Your conditional print statement will get executed during processing of the require but $path won't get defined until import gets called later.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Then how could I possibly use the value of $path? Must I put everything into the import subroutine? If so, then I have another question:
      sub import { my $self = shift; my %args = @_; $path = $args{path}; open(FILEREAD, "$path") or die "Cannot open $path for reading: $!\n"; read FILEREAD, $string, -s FILEREAD; close FILEREAD; @matches = $string =~ /\s.*\s.*\@.*\s/g; foreach (@matches) { if (/\s(.*)\s(.*\@.*)\s/) { $users{$1} = $2 } }
      From the above code, how could I export the users hash? I put @EXPORT = qw(%users); near the top of my code but it didn't work. I then tried $Bio->export_to_level(1 @_) but that didn't work either.
        Then how could I possibly use the value of $path? Must I put everything into the import subroutine?
        No, not everything... You can use it in any other method that you call at run time. But top level code runs at compile time — i.e. in the require step. Before the import step, just like pfaut said.

        Just print the "Hello" in the import routine.

        From the above code, how could I export the users hash?
        In import, call SUPER::import. Or Exporter::import. Just make sure the arguments to the call are right.
        package Bio; use strict; use warnings; use vars qw($VERSION %USERS); sub import { my $package = shift; my %args = @_; my $users_file = $args{path}; open USERS, $users_file or die "Cannot open $users_file for readin +g: $!"; my $users_data = do { local $/; <USERS> }; close USERS or die "Cannot close USERS file handle: $!"; # Write to the global variable %USERS = $users_data =~ m{(\S+)\s+(\S+\@\S+)}g; } 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found