I'd like all the classes in my large package to all have the same $VERSION (as defined in $My::Package::Version::VERSION). This is easy with Exporter and @EXPORT = qw($VERSION); all of my classes must then simply "use My::Package::Version".

But I wanted to make it a little easier by only having to add the "use" statement to the small handful of base classes (rather than the nearly hundred other classes that inherit from them). I tried to work up this idea:

package My::Package::Version; use strict; use vars qw(@ISA $VERSION @EXPORT); @ISA = qw(Exporter); $VERSION = 3.14; @EXPORT = qw($VERSION); sub import { # try to handle multiple levels of inheritance: my $i = 0; my $pkg = caller($i); while ($pkg && $pkg =~ m/^My::Package::/o && !defined ${"$pkg::VERSION"}) { __PACKAGE__->export_to_level($i+1); $pkg = caller(++$i); } } 1;
The problem, of course, is that this doesn't work; even though My::Package::A uses My::Package::B (and ISA ::B), which itself uses My::Package::Version, only $B::VERSION gets set, not $A::VERSION (since caller(2) becomes "main", not My::Package::A when A compiles B).

Is there some tricky solution to this?

Thanks,

-Aaron


In reply to variable "inheritance"; viral $VERSION by amackey

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.