Exporting Module Functions
qazwart
created: 2006-06-05 16:01:09
I'm having a bit of trouble writing a module. My module has a function call "guid()". When I attempt to call it from my program, I get the following error:
Undefined subroutine &main::guid called at H:\bin\foo.pl line 11.
However, if I call my function as Guid::guid(), it works. Here's the calling program that doesn't work:
use Guid;
print "Guid = " . guid() . "\n";
Nor does this work:
use Guid qw(guid);
print "Guid = " . guid() . "\n";
But, this does work:
use Guid;
print "Guid = " . Guid::guid() . "\n";
Here's my module's header (from Guid.pm):
package Guid;
use strict;
use warnings;

use Math::BigInt;
use Net::Address::Ethernet qw(get_address);

BEGIN {
    use Exporter ();
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
    $VERSION = 1.00;
    @EXPORT = qw(guid);
    %EXPORT_TAGS = ();
}


sub guid {
    my ($clockId, $netId) = $_;

    my $maxClockId = 2**14;
    my $clockIdOffset = 2**15;  #Turn on Clock High bit
    my $timeOffset = "122192928000000000";
    [...And On...]
I've written Perl modules before and have successfully exported the various functions in them. I know the problem is probably something very simple, but I can't figure out what I am doing wrong. So, why can't I seem to export guid() to my main namespace? What stupid detail am I leaving out?
Re: Exporting Module Functions
created: 2006-06-05 16:12:34

You forgot to add Exporter to the @ISA of the package.

Re: Exporting Module Functions
created: 2006-06-05 16:12:45

Your module never sets @Guid::ISA to indicate that it ISA Exporter, so the Exporter::import method never gets called on its behalf. See the Exporter documentation for more details.

Re: Exporting Module Functions
created: 2006-06-05 17:30:50
use base qw(Exporter);

see base
also I don't see the need for a begin block here.

Re: Exporting Module Functions
created: 2006-06-06 09:04:23
Yup. That was the problem. I forgot "@ISA = qw(Exporter);" which establishes that my module can export functions.

I've sat there for days trying to figure this out and comparing my header of this module to headers of other modules. For some reason, I didn't notice this one missing line.

Thanks.

perlmonks.org content © perlmonks.org and ambrus, Fletch, qazwart, tinita

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03