in reply to Constants and Subclasses...
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Constants and Subclasses...
by diotalevi (Canon) on Dec 19, 2006 at 22:18 UTC | |
by t'mo (Pilgrim) on Dec 22, 2006 at 18:53 UTC | |
by diotalevi (Canon) on Dec 22, 2006 at 18:56 UTC |
In Section
Seekers of Perl Wisdom