in reply to variable "inheritance"; viral $VERSION
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: variable "inheritance"; viral $VERSION
by amackey (Beadle) on Mar 09, 2003 at 13:14 UTC |