Confused by exporting subroutines

P

Prototype

Hi

I'm in the process of converting some perl code to an OO style, and
have come across something that's rather confused me. I've basically
got one package ("apackage") which contains some subroutines, one of
which creates an object from another package "bpackage". However,
"bpackage" requires a subroutine from "apackage" in one of its methods,
but doesn't seem able to find it. and I get an "underfined subroutine"
error. If I move this required subroutine to another package "cpackage"
then there is no problem, so it's something to do with the subroutine
being in the "apackage" where "bpackage" has been invoked from.

Perhaps a bit clearer with some code. There's three files, which I've
posted at the bottom of this post which demonstrate the problem.

Now I realise what I'm trying to do here is not good programming style,
and once the code is fully OO this issue should disappear. But at the
same time I'm confused as to what's actually happening, and would like
to understand what's going on and causing the error.

Thanks for any help or pointers!

Cheers

Paul.

(Perl 5.8.3 on i586 SuSE linux)


-----------------------------------------------
main.pl:
-----------------------------------------------
#!/usr/bin/perl -w
use strict;
use apackage;
a();

-----------------------------------------------
apackage.pm:
-----------------------------------------------
package apackage;
use strict;

use Exporter;
use vars qw(@ISA @EXPORT);
@ISA=qw(Exporter);
@EXPORT=qw(&a &helloworld);

use bpackage;

sub a {
my $object;
print "In sub a\n";
$object=bpackage->new;
print "a: object is $object\n";
}

sub helloworld {
print "helloworld from apackage\n";
}

1;

-----------------------------------------------
bpackage.pm:
-----------------------------------------------
package bpackage;
use apackage;

sub new {
my $class=shift;
my $self = {};
bless ($self,$class);
helloworld();
return $self;
}
1;
 
A

Anno Siegel

Prototype said:
Hi

I'm in the process of converting some perl code to an OO style, and
have come across something that's rather confused me. I've basically
got one package ("apackage") which contains some subroutines, one of
which creates an object from another package "bpackage". However,
"bpackage" requires a subroutine from "apackage" in one of its methods,
but doesn't seem able to find it. and I get an "underfined subroutine"
error. If I move this required subroutine to another package "cpackage"
then there is no problem, so it's something to do with the subroutine
being in the "apackage" where "bpackage" has been invoked from.

When you find that your modules are mutually importing from each
other, it's time to step back and look what's gone wrong. There
*may* be situations where this makes sense, but usually it can
be avoided. If it can, it should.
Perhaps a bit clearer with some code. There's three files, which I've
posted at the bottom of this post which demonstrate the problem.

Now I realise what I'm trying to do here is not good programming style,
and once the code is fully OO this issue should disappear. But at the
same time I'm confused as to what's actually happening, and would like
to understand what's going on and causing the error.

In this case the problem is that when apackage uses bpackage it has
not yet set @ISA and @EXPORT. The "use bpackage" below is executed
at compile time, before the assignments are done. Now bpackage
tries to import from apackage, but fails because apackage isn't
prepared. Put BEGIN {} around the assignments to @ISA and @EXPORT
and it will work as intended. Then get rid of the mutual importation.
In fact, if you are going for OO, you shouldn't need Exporter at
all.

BTW, situations like this are probably the motivation why code
examples often show BEGIN {} around assignments to @ISA, @EXPORT
and similar. It isn't always needed, but if it is, it can be hard
to find what's wrong.

Anno

[code left below for reference]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top