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

Greetings,

I am designing a module whose behavior is dictated by the import list. But I want the import list to act just like @ARGV. Here's an example of what I want:

use Some::Module ("http://www.perlmonks.org");

In the example above, I want to capture the URL to use as I please within the module. I know that this isn't what the import list is for, but I would like it to behave in this fashion. How would I do this?

Replies are listed 'Best First'.
Re: Reading and capturing the import list
by sauoq (Abbot) on Jul 03, 2003 at 04:23 UTC

    The module's import() sub is called with the list specified when the module is used. So, something like this will do:

    package Foo; sub import { my $class = shift; # It's called as a class method. @Foo::urls = @_; } 1;

    Update: See PodMaster's comments below. He is entirely correct. I've edited the code above to reflect that. (And, thanks PodMaster, for catching my thinko.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      It is a method, so the 1st argument will be the package/class name ;)
      C:\>perldoc -f import
          import  There is no builtin "import" function. It is just an ordinary
                  method (subroutine) defined (or inherited) by modules that wish
                  to export names to another module. The "use" function calls the
                  "import" method for the package used. See also "use", perlmod,
                  and Exporter.
      
      jacques (or anyone wanting to learn about modules) should read `perldoc -f perlmod' and the documentation it references.

      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.

      I don't have to worry about the package not finding a subroutine called "http://www.perlmonks.org"?

        No. It's a generic mechanism. It is only by convention that we use it to export sub names and global variables. It can be easily adapted to do what you are trying to do.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Reading and capturing the import list
by bobn (Chaplain) on Jul 03, 2003 at 06:04 UTC
    Would it not make more sense to leave the import mechanism alone and just do it in the constructor, lile everyone else?
    package Some::Module; my @urls; sub new { my $class = shift; my @urls = @_ if @_; bless {}, $class; } ##### main file : use Some::Module; $s = Some::Module->new("http://www.perlmonks.org");


    --Bob Niederman, http://bob-n.com

      If he's writing an OO module, perhaps. Not all modules are OO. Think constant, for example. :)