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;
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;