<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi all,
>
> I have a module which does nothing but include a bunch of use statements
(Shawn
> Corey, I think you taught me how to do that).It looks like this:
>
> --------------------------------------------------------------------------
--------
> #PerlMQ.pm
>
> use strict;
> use warnings;
>
> use statement1;
> use statement2;
> use statement3;
>
> #and so on...
>
> 1;
>
> _END_
>
> --------------------------------------------------------------------------
------
>
> In my script, I load the module by saying "use PerlMQ;". However, now I
want to
> load only certain use statements from the module depending on the
parameter I
> give it. For example, in my script, I want to say "use PerlMQ
> qw(some_parameter)" in order to load the use statements specified by
> some_parameter in the module. Is it possible to do that?
>
> Most of the examples on the web seem to use the EXPORT package while
providing
> the qw(some_parameter) functionality in the script. How do I do it in my
module
> which seems pretty unconventional?
>

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:

$ 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