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

Hi all. I'd Like to write something like: use MyPackage qw/arg1 arg2/ ; ... How should I write MyPackage.pm to access the args given ? Thx for help !

Replies are listed 'Best First'.
Re: package parameters
by trammell (Priest) on Apr 01, 2005 at 15:09 UTC
    From perldoc -f use:
      [use] is exactly equivalent to
    
        BEGIN { require Module; import Module LIST; }
    
      except that Module must be a bareword.
    
    So if you have Bar.pm:
    package Bar; sub import { warn "Arguments are: @_"; } 1;
    When you use Bar, you get:
    % perl -e 'use Bar qw/abc def/'
    Arguments are: Bar abc def at Bar.pm line 6.
    
Re: package parameters
by Golo (Friar) on Apr 01, 2005 at 15:04 UTC
    You need to define an import sub, which will get the arguments to your package.
    package MyPackage; sub import { my $class = shift; print "args: @_\n"; } 1; package Main; use MyPackage qw(arg1 arg2);
Re: package parameters
by Thilosophy (Curate) on Apr 02, 2005 at 06:07 UTC
    use MyPackage qw/arg1 arg2/ ;
    If this is supposed to export subroutines arg1 and args2, you can use the Exporter standard module:
    package MyPackage; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(arg1 arg2); # symbols to export on request
    Of course, if you want to do something else with those args, you can get creative with your custom import subroutine as shown by Golo and trammell above.