Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Perl Inheritance & module variables

by gone2015 (Deacon)
on Sep 18, 2008 at 16:30 UTC ( #712331=note: print w/replies, xml ) Need Help??

in reply to Perl Inheritance & module variables

So, you have a module called Foo::Bar and objects of the class Foo::Bar, and you want to find the version of the module associated with the object, yes ? For example, while doing some superclass stuff ?

Which would be something along the lines:

package Foo::Bar ; ..... our $VERSION = "0.00" ; .....
and somewhere you have:
my $obj = new Foo::Bar (...) ;
and somewhere else you want to find the $VERSION associated with $obj ?

Suppose you had Foo/

package Foo::Bar ; use strict ; use warnings ; our $VERSION = "1.01" ; use base qw(Foo); sub new { return bless [], shift ; } ; 1 ;
package Foo ; use strict ; use warnings ; our $VERSION = "0.11" ; sub version { my ($self) = @_ ; no strict 'refs' ; # NB: seatbelt off. return ${ref($self)."::VERSION"} ; # "Symbolic Reference" } ; 1 ;
use strict ; use warnings ; use Foo::Bar ; my $obj = new Foo::Bar ; print $obj->version(), "\n" ;
should print:
which may have been what you had in mind.

It may also be what kyle just recommended NOT to do ?

Replies are listed 'Best First'.
Re^2: Perl Inheritance & module variables
by diabelek (Beadle) on Sep 19, 2008 at 16:26 UTC

    Well that does what I want but I really hate having to turn off the strict refs... I think I would prefer to just copy the data into the instance and deal it with that way unless there is a lot of data i need to copy or its time sensitive. Unless there is a better way of doing it, I think I might just keep that in my back pocket for when I really do need it.

    Thanks a bunch

      Wishing to maintain "strict refs purity" is entirely commendable :-)

      For completeness, however, I'll point out that the 'no strict'-ness is scoped within the current block. So the unpleasantness is contained. In the code I gave the the nastiness was enclosed in a subroutine. If it's necessary to do this in 'open code' you can always:

      { no strict 'refs' ; .... # unspeakable stuff redacted } ;
      to avoid contagion

      Anyway, I think that with a small penance, in the form, say, of an explanatory comment, one can forgive oneself this particular sin -- provided it doesn't become a habit :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://712331]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2018-07-19 00:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (398 votes). Check out past polls.