Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Constants and Subclasses...

by t'mo (Pilgrim)
on Dec 18, 2006 at 18:19 UTC ( [id://590511] : perlquestion . print w/replies, xml ) Need Help??

t'mo has asked for the wisdom of the Perl Monks concerning the following question:

How do I create a constant in a parent class that I can use "simply" from a child class? E.g., I'm trying to avoid having to use $self->FOO at the same time I'm trying to avoid this warning:

Bareword "EMPTY_FLAG" not allowed while "strict subs" in use...
package ParentClass; use constant FOO => 'foo'; ... package ChildClass; use base 'ParentClass'; sub doStuff { my ($self, @otherArguments) = @_; ... #print FOO; # dang! this generates the 'Bareword "EMPTY_FLAG" not al +lowed...' error print $self->FOO; ... }

P.S. I looked already but didn't find an answer. If somebody's already answered this, please feel free to point me to that answer.

Replies are listed 'Best First'.
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.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

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.

      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.]

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

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

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        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.

      Does require call the import()? In any case, I was about to answer the Exporter, but I think to make that less sucky you'd want to use EXPORT_OK and/or EXPORT_TAGS instead. So the child class could use them explicitly if it wanted to. That's probably pretty obvious I guess.

      But, I always use base qw(Exporter) to get the exporting to actually happen. I've never tried it with require. Does the require work? [update: thanks ferreira]


        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.

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.

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

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊