Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

It may be silly, but it seems that constants should come along with a base class just like methods. Consequently, I would like to access a superclass' constants without having to "use" a second file.

I looked at this question, but it doesn't float my boat. I guess if that's what exporter and constant are doing behind the scenes, it would be OK, but it is certainly non-intuitive.

So, what is the right way to inherit constants from a super-superclass?

Something along the lines of this:

# Foo/Constants.pm package Foo::Constants; use constant PI => 3; # Foo/Base.pm package Foo::Base; use Foo::Constants; # Foo/Sub.pm package Foo::Sub; use base "Foo::Base"; # Bar/Sub.pm package Bar::Sub; use base "Foo::Sub"; use strict; sub new { print PI; #fails, this is the one I want print Bar::Sub::PI; #fails print Foo::Sub::PI; #fails print Foo::Base::PI; #fails print Foo::Constants::PI #even this fails }

Of course, I could could "use Foo::Constants" in all those files, but, ideally, I would like to have a subclass of Foo::Constants (e.g. Bar::Constants) that would export even more things without having to re-export everything in it's superclass.

Thanks!

Replies are listed 'Best First'.
Re: Any way to inherit constants?
by Zaxo (Archbishop) on May 15, 2005 at 05:13 UTC

    The problem is that Foo::Base is not importing the constant subs from Foo::Constants. You can say

    package Foo::Base; use Foo::Constants qw/PI FOO QUUX/;
    listing each constant function you want to import, or dress up Foo::Constants with Exporter to define exports and tags for them.

    After Compline,
    Zaxo

      Whoops, I forgot what I actually have written. Foo::Constants should have looked like this:

      # Foo/Constants.pm package Foo::Constants; use base "Exporter"; use constant PI => 3; our @EXPORT = (PI);
      As such, I can use PI fron within any package that does "use Foo:Constants", but not from any of its subclasses. Thanks!
        agreed! this is the way the problem can be solved the perlish way.
        language is a virus from outer space.
Re: Any way to inherit constants?
by bart (Canon) on May 15, 2005 at 08:52 UTC
    Spiffy

    OK, the docs aren't exactly crystal clear on what I'm talking about, so let me point you to the article that introduced me, and lots of others, to Spiffy: Simple IO Handling with IO::All. Look for the section entitled "It's Not Just Keen, It's Spiffy":

    Its primary magic trick is that it supports a unique feature that I call inheritable exporting.
Re: Any way to inherit constants?
by tlm (Prior) on May 15, 2005 at 14:26 UTC

    The thingies one creates with use constant are subs, so you could just inherit them like any other method:

    use strict; use warnings; package Foo::Constants; use constant PI => 3.1415826; package main; @Foo::Base::ISA = qw( Foo::Constants ); @Foo::Sub::ISA = qw( Foo::Base ); @Bar::Sub::ISA = qw( Foo::Sub ); sub Bar::Sub::new { my $class = shift; print $class->PI, $/; } Bar::Sub->new(); __END__ 3.1415926

    the lowliest monk