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

Re: Export/import variables

by pfaut (Priest)
on Mar 18, 2003 at 03:03 UTC ( #243861=note: print w/replies, xml ) Need Help??


in reply to Export/import variables

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';

Replies are listed 'Best First'.
Re: Re: Export/import variables
by Anonymous Monk on Mar 18, 2003 at 03:30 UTC
    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
Node Status?
node history
Node Type: note [id://243861]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2021-04-14 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?