Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Finding version of a Perl module

by ikegami (Patriarch)
on Sep 09, 2005 at 14:07 UTC ( [id://490578]=note: print w/replies, xml ) Need Help??


in reply to Re: Finding version of a Perl module
in thread Finding version of a Perl module

Caveat: merlyn's trick only works is the module uses Exporter or immitates Exporter's version checks (Added for clarity:) by writting an import which calls UNIVERSAL::VERSION.

Update: Apparently, perl will call UNIVERSAL::VERSION for you if the version is passed as a number, not a string.

Replies are listed 'Best First'.
Re^3: Finding version of a Perl module
by itub (Priest) on Sep 09, 2005 at 14:51 UTC
    I don't think so. The version check is done by UNIVERSAL::VERSION , which is called by use when you specify a version number when using a module.

    From "perldoc -f use":

    If the VERSION argument is present between Module and LIST, then the "use" will call the VERSION method in class Module with the given version as an argument. The default VERSION method, inherited from the UNIVERSAL class, croaks if the given version is larger than the value of the variable $Module::VERSION.
      Yes, but you need something to call UNIVERSAL::VERSION. Usually, that something is Exporter. Proof:
      # Module1.pm # ========== package Module1; our $VERSION = '2.0'; 1;
      # Module2.pm # ========== package Module2; use Exporter (); our $VERSION = '2.0'; our @ISA = 'Exporter'; 1;
      # script.pl # ========= BEGIN { print("Loading Module1...\n"); } use Module1 '3.0'; BEGIN { print("Loaded.\n"); } BEGIN { print("Loading Module2...\n"); } use Module2 '3.0'; BEGIN { print("Loaded.\n"); }
      output ====== Loading Module1... Loaded. Loading Module2... Module2 3.0 required--this is only version 2.0 (Module2.pm) at script. +pl line 6 BEGIN failed--compilation aborted at script.pl line 6.

      Notice how the version passed to Module1 is completely ignored.


      In writing this test, I also found errors in what I thought I knew.

      1) *import = \&Exporter::import; doesn't work as well as I thought. My original Module2 gave a run-time error. It was:

      package Module2; use Exporter (); our $VERSION = '2.0'; *import = \&Exporter::import; 1;

      To fix without inheriting from Exporter, one needs to also import require_version:

      package Module2; use Exporter (); our $VERSION = '2.0'; *import = \&Exporter::import; *require_version = \&Exporter::require_version; 1;

      2) I always thought use Module 'maj.min' would make sure the major version is the same as the one in $VERSION, yet use Module2 '1.0' doesn't fail.

        That's interesting. But if you say use Module1 3.0; (notice that the version number is not in quotes), then you get
        Loading Module1...
        Module1 version 3 required--this is only version 2.0 at version.pl line 4.
        BEGIN failed--compilation aborted at version.pl line 4.
        

        It seems that if you give a number, the version check is done by use, but if you give a string it is treated as the first argument to pass to import, in which case you do need exporter to handle it.

        I hadn't noticed because I always call use by giving the version numbers as numbers, not as strings.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://490578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-29 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found