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!


In reply to Any way to inherit constants? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.