How can I share variables between a base class and subclass in Perl?
Posted
by Jonathan
on Stack Overflow
See other posts from Stack Overflow
or by Jonathan
Published on 2010-04-20T00:49:38Z
Indexed on
2010/04/20
1:13 UTC
Read the original article
Hit count: 406
I have a base class like this:
package MyClass;
use vars qw/$ME list of vars/;
use Exporter;
@ISA = qw/Exporter/;
@EXPORT_OK = qw/ many variables & functions/;
%EXPORT_TAGS = (all => \@EXPORT_OK );
sub my_method {
}
sub other_methods etc {
}
--- more code---
I want to subclass MyClass
, but only for one method.
package MySubclass;
use MyClass;
use vars qw/@ISA/;
@ISA = 'MyClass';
sub my_method {
--- new method
}
And I want to call this MySubclass
like I would the original MyClass
, and still have access to all of the variables and functions from Exporter
. However I am having problems getting the Exporter
variables from the original class, MyClass
, to export correctly. Do I need to run Exporter
again inside the subclass? That seems redundant and unclear.
Example file:
#!/usr/bin/perl
use MySubclass /$ME/;
-- rest of code
But I get compile errors when I try to import the $ME
variable. Any suggestions?
© Stack Overflow or respective owner