Re: Constants and Subclasses...
by diotalevi (Canon) on Dec 18, 2006 at 18:31 UTC
|
You have to say $obj->FOO or Class->Foo if you want to use inheritance. If you don't, then Class::FOO is sufficient and uses the compile-time optimization. The inheritance-respecting versions don't and can't.
| [reply] [d/l] [select] |
Re: Constants and Subclasses...
by ferreira (Chaplain) on Dec 18, 2006 at 18:30 UTC
|
I think you can make it work if you derive your base class from an exporting-capable module, like Exporter. In this case, declare an @EXPORT array with the things you want the children classes to know about. In the code of the child class, invoke import() so that FOO can be known without further qualification.
package ParentClass;
use base qw(Exporter); # requires and adds Exporter to @ISA
BEGIN { our @EXPORT = qw(FOO); } # what must be known elsewhere
use constant FOO => 'foo';
package ChildClass;
use base 'ParentClass';
BEGIN { P->import(); } # import FOO
sub doStuff {
my ($self, @otherArguments) = @_;
print FOO; # should work just fine
print $self->FOO; # just the same as above
}
Update: jettero and diotalevi pointed the flaws in my first versions of this answer. It was much harder than I thought at first to get it right. The BEGIN's blocks are absolutely necessary to make it work. | [reply] [d/l] [select] |
|
|
That's not going to work for anything that inherits from ChildClass. It also doesn't work if the inheritance relationship changes after compile-time. [Added: You also forgot to import &import from Exporter or make the parent class an ISA of Exporter.]
| [reply] |
|
|
| [reply] |
|
|
|
|
BEGIN is only necessary because you're doing this all in a single file and need to hack the order of execution. This wouldn't normally be an issue in real code. Also, if you switched to use Parent; @ISA = 'Parent' instead of base.pm then you wouldn't need to separately call import. Further... making the parent export it's constants means that all other code that merely uses Parent will also get the constants exported and that's likely *not* what you want.
| [reply] |
|
|
I understand this more fully now after sleeping over it and after your excellent explanation and alternative with use Parent; @ISA = 'Parent'. (But — in the single file case — realize I cannot use because that will call require which will try to read Parent from a file. In turn, use base apparently doesn't do this if it notices the package is already there — not relying only on %INC.)
With respect to the unwanted exportation of the constants, it might be better to implement this as suggested by jettero (Re^2: Constants and Subclasses...) and do:
our %EXPORT_TAGS = (
CONSTANTS => [qw(FOO)], # and other constants
);
our @EXPORT_OK;
Exporter::export_ok_tags('CONSTANTS'); # populate the @EXPORT_OK
and then
BEGIN { P->import(':CONSTANTS'); }
As the constants are not exported by default anymore, the user of the code should explicitly call for them or not.
Yeah, jettero, you were right from the beginning about the convenience of @EXPORT_OK and %EXPORT_TAGS. | [reply] [d/l] [select] |
|
|
|
|
| [reply] [d/l] [select] |
|
|
No, require does not call import(). And you reminded me I wanted to include Exporter in the @ISA array so that the parent class inherited the import() method from Exporter (which is why this module came into the answer). This is almost equivalent to use base qw(Exporter) (but not identical). See the docs of base.
Also, if the symbols were added to @EXPORT_OK, they should be explicitly mentioned as arguments of import. To keep the answer in line with the question, using @EXPORT is the easiest thing to do.
| [reply] |
Re: Constants and Subclasses...
by t'mo (Pilgrim) on Dec 18, 2006 at 22:38 UTC
|
Thanks for the suggestions. My code is evolving somewhat differently than the suggested code (mostly to avoid the duplication implicit in both use const NAME and putting NAME in @EXPORT). I agree that it's unfortunate that the namespace is polluted via all the Exporter stuff, but hey, it works. (Note: ParentClass is defined in another file, I just didn't mention it before.)
package ParentClass;
BEGIN {
my %constants = (
FOO => 'foo',
BAR => 'bar',
BAZ => 'baz',
);
foreach my $name ( keys %constants ) {
no strict 'refs';
*{$name} = sub () { $constants{$name} };
}
our @EXPORT = keys %constants;
}
# -------- new file --------
package ChildClass;
use base 'ParentClass';
BEGIN {
ParentClass->import();
}
sub doStuff {
my ($self, @otherArguments) = @_;
print FOO; # Yay, it works this time!
...
}
Edit: changed scalar constants to hash.
| [reply] [d/l] |
|
|
You wrote:BEGIN {
my $constants = (
FOO => 'foo',
BAR => 'bar',
BAZ => 'baz',
);
foreach my $name ( keys %constants ) {
no strict 'refs';
*{$name} = sub () { $constants{$name} };
}
...
}</blockquote>
If you're going to make constants, do them the proper way with the constant pragma. What you wrote is roughly how constant.pm works in versions prior to 5.9 but the next versions of perl have a different implementation for constant and you'll want your code to do the right thing regardless.BEGIN {
require constant;
my %constants = (
FOO => 'foo',
BAR => 'bar',
BAZ => 'baz',
);
foreach my $name ( keys %constants ) {
constant->import( $name => $constants{$name} );
}
}
You also goofed and made a variable $constants instead of %constants.
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|