The answer to your specific question as to why your code doesn't export the version to My::Package::A is that, as you already noted, caller(2) is main for some reason. However, if you do a full call stack trace from the import, you will see that My::Package::A is included but there are two instances of main in between it. I don't really have any idea why that is (perhaps a more enlightened monk can explain) but this causes the condition of your while loop to fail and thus not go futher into the call stack.

I believe what you want, and probably intended to do, is to simply skip packages not begining with My::Package but continue the loop. Like so:

./My/Package/Version.pm:

package My::Package::Version; use strict; use vars qw(@ISA @EXPORT $VERSION); use Exporter; @ISA = qw(Exporter); @EXPORT = qw($VERSION); $VERSION = 3.14; sub import { my $i = 0; my $pkg = caller(0); print "Import Caller Trace:\n"; while($pkg) { print " Called from package $pkg\n"; # Note that these tests have been moved from the # loop condition to inside the loop so the loop # will not terminate if it fails. if($pkg =~ /^My::Package::/ and not defined ${"$pkg\::VERSION"}) { print " (Exporting VERSION to $pkg)\n"; __PACKAGE__->export_to_level($i+1); } $pkg = caller(++$i); } }

./My/Package/B.pm:

package My::Package::B; use My::Package::Version; print "My::Package::B loaded.\n"; 1;

./My/Package/A.pm:

package My::Package::A; use My::Package::B; @ISA = ('My::Package::B'); print "My::Package::A loaded.\n"; 1;

./tst.pl:

#!/usr/bin/perl use lib '.'; use My::Package::A; print "\$My::Package::A::VERSION = $My::Package::A::VERSION\n";

Output of ./tst.pl:

Import Caller Trace: Called from package My::Package::B (Exporting VERSION to My::Package::B) Called from package main Called from package main Called from package My::Package::A (Exporting VERSION to My::Package::A) Called from package main Called from package main Called from package main Called from package main Called from package main My::Package::B loaded. My::Package::A loaded. $My::Package::A::VERSION = 3.14

bbfu
Black flowers blossum
Fearless on my breath


In reply to Re: variable "inheritance"; viral $VERSION by bbfu
in thread 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.