Todd W wrote:

[snip]

> you could do something like this:
> 
> $ cat TestMod.pm
> use warnings;
> use strict;
> 
> package TestMod;
> 
> use Exporter;
> 
> sub import {
>   my $class = shift;
>   foreach my $module ( @_ ) {
>     require $module;
>   }
> }
> 
> 1;
> 
> $ cat testrun.pl
> use warnings;
> use strict;
> 
> use TestMod qw( CGI.pm LWP/Simple.pm );
> 
> print( UNIVERSAL::can( CGI => 'new' ), "\n" );
> print( UNIVERSAL::can( 'LWP::Simple' => 'get' ), "\n" );
> 
> saying "use Exporter;" in your module means that when your module is use()ed
> by a client program, a function in your module named "import" will be called
> by Exporter if this import function exists. Heres the output of running that
> program:
> 

Just for your info, the call to 'import' has nothing to do with the use
of Exporter. Exporter actually provides a special 'import' method that
does fancy stuff, but the actual calling of 'import' happens *everytime*
you do a 'use' (assuming the module 'can', aka has a sub named 'import')
regardless of whether Exporter has been loaded. And to have this
actually work you would have to inherit from Exporter so that 'import'
can be found in the sub lookup. Though as you have shown you can also
write your own import sub, which can be quite handy.

perldoc -f use
perldoc Exporter

for more info.

http://danconia.org

> $ perl testrun.pl
> CODE(0x818459c)
> CODE(0x8129448)
> 
> which shows that after saying "use TestMod qw( CGI.pm LWP/Simple.pm );", a
> package named CGI can new() and a package named LWP::Simple can get(). It
> should be pretty safe too, because require() will die if it cant find the
> module.
> 
> I just made this up and I've never done anything like this before ( I
> program in mod_perl so I just use() everything I will ever need ), so there
> may be some caveats that aren't immediately obvious to me.
> 
> Enjoy,
> 
> Todd W.
> 
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to