http://qs321.pair.com?node_id=1092253

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

What is the best run-time equivalent of use Module VERSION? eval "use Module 1.23" works but it involves string eval. require Module; Module->import(1.23) doesn't work. require Module; Module->VERSION(1.23) works only for modules which install a VERSION() method.

Replies are listed 'Best First'.
Re: Run-time equivalent of "use Module VERSION"?
by Corion (Patriarch) on Jul 04, 2014 at 07:50 UTC

    VERSION is magic and always there for you:

    > perl -wle "package VersionTest; use vars qw($VERSION); $VERSION=666; + package main; print VersionTest->VERSION(666); print VersionTest->VE +RSION(667);" 666 VersionTest verion 667 required--this is only version 666 at -e line 1 +.
      > VERSION is magic and always there for you:

      elaborating a bit from perlobj#VERSION($need) :)

      UNIVERSAL::VERSION() is like ->can() a "universal"¹ method and ...

      This method is called automatically by the "VERSION" form of "use".
      use A 1.2 qw(some imported subs); # implies: A->VERSION(1.2);

      Cheers Rolf

      (addicted to the Perl Programming Language)

      ¹) All classes automatically inherit from the UNIVERSAL class, which is built-in to the Perl core.

Re: Run-time equivalent of "use Module VERSION"?
by CountZero (Bishop) on Jul 04, 2014 at 13:50 UTC
    Module->VERSION(1.23) works only for modules which install a VERSION() method.

    That is not entirely true as LanX already explained. However, it assumes that the module provides in some standard way a version number that can be understood and compared. In that respect nothing is guaranteed.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Run-time equivalent of "use Module VERSION"?
by DrHyde (Prior) on Jul 04, 2014 at 12:10 UTC
    So what if it involves string eval?
Re: Run-time equivalent of "use Module VERSION"?
by McA (Priest) on Jul 04, 2014 at 09:54 UTC