On Thu, 2005-03-31 at 12:41 -0600, JupiterHost.Net wrote:
> Hello,
> 
> I am working on a module that needs to import a function from 
> Digest::MD5. The reasoning on this is that its a subclass so it'd be 
> handy to be able to use() it and export functions from the parent module 
> without having to add a
> sub foo { Parent::foo(@_) }
Add the parent module/class to the perl @ISA array. Perl will search the
@ISA array for any methods that aren't available in the current package.
In the example of your module, it is technically a subclass of Exporter
because you've added that you your @ISA array. If you simply add
'Digest::MD5' to the @ISA array you'll inherit all the methods that it
provides. For you to be able to call md5_base64 from within test.pl
without qualifying it (and you want it to call the FooTest.pm inherited
version of it, you'll have to Export the method (perldoc Exporter) by
adding it to either the @EXPORT_OK or @EXPORT array in FooTest.pm. The
@EXPORT array automatically exports the methods listed while the
@EXPORT_OK array only allows importing the methods by adding them after
the module name on the 'use' line as shown below. Adding anything into
the @EXPORT array is discouraged as that will trample someone else's
namespace, i.e. test.pl in this case.

I hope that helps. 

...[snip]...

> file test.pl:
> #!/usr/bin/perl
> 
> use strict;
> use warnings;

use FooTest qw(md5_base64);

> print FooTest::funk();
> print Digest::MD5::md5_base64('howdy'),"\n";
> print md5_base64('howdy'),"\n";
> 
> file FooTest.pm:
> package FooTest;
> 
> use strict;
> use warnings;
> use Digest::MD5;
> 
> require Exporter;
our @ISA = qw(Exporter, Digest::MD5);
our @EXPORT_OK = qw(funk md5_base64);
> 
> sub import {
>     $_[0]->export_to_level(2, grep(!/^-/, @_));
>     shift;
>     Digest::MD5->import('md5_base64')
> }
> 
> sub funk { md5_base64('howdy'),"\n" }
> 
> Output is:
> B4Lv1ht6awLmAsxqEWc+yQ
> B4Lv1ht6awLmAsxqEWc+yQ
> Undefined subroutine &main::md5_base64 called at test line 9.
> 
> Ouput Should be:
> B4Lv1ht6awLmAsxqEWc+yQ
> B4Lv1ht6awLmAsxqEWc+yQ
> B4Lv1ht6awLmAsxqEWc+yQ
> 
> i know Digest::MD5->import() is being called becasue if you add 
> something that it does not export you get teh error about it not being 
> exported by that module.
> 
> it just importing it to FooTest (IE see funk()) but I need it imported 
> to the where ever it was use()ed from.
> 
-- 
Joshua Colson <[EMAIL PROTECTED]>
Sr. Systems Administrator
Giant Industries, Inc
P: (480) 585-8714
F: (480) 502-6641

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to