in reply to Re: Export/import variables
in thread Export/import variables

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.

Replies are listed 'Best First'.
Re: Re: Re: Export/import variables
by bart (Canon) on Mar 19, 2003 at 21:08 UTC
    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.
Re: Re: Re: Export/import variables
by diotalevi (Canon) on Mar 19, 2003 at 21:11 UTC
    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;