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

If I say something like (for example):

use MIME::Types 1.005;

Is Perl smart enough to look in all the library directories in @INC that have a copy of MIME::Types to see if one of multiple occurances of the same module is newer (or newer enough) to be used?

This seems to fall in the "Do what I say" boat, but I just want to make sure. It's sort of fuzzy to me what the require_version() function in Exporter actually does.

  • Comment on Module Version Checking - Checking Multiple Installations of the Module
  • Download Code

Replies are listed 'Best First'.
Re: Module Version Checking - Checking Multiple Installations of the Module
by bart (Canon) on Jan 19, 2003 at 09:56 UTC
    I'm not sure, I think not. I think that you have to take care that the latest version is found first, because all perl does, is load the first instance it finds, and then check the version. Since $VERSION is just a plain variable that is set by perl code while loading the module, that seems to be the only way to do it that makes sense: you need to load the module to get at the version number.

    I do recall that there has been discussion to add your requested feature to Perl6, that it will be possible to have multiple versions of modules with the same name, and you to select the version you want. I think it'll even possible to have multiple versions of a module loaded at the same time, in the same script, which most definitely isn't possible in perl5, is their package contents (they use the same package name) would clash.

    Let's just say that for Perl5, Larry was a bit naive, assuming, just like Microsoft did with its versioning of system DLL's, that newer versions would always be backward compatible with older ones, and that all you ever needed was the newer version. However, this cannot be kept up in the long run. It has bitten me already, now that HTML::Parser version 3 isn't even compatible with version 2 any more, as the "text" callback has more arguments and thus the "last argument" is something entirely different between the two module versions. As a result, I can't upgrade without rewriting my scripts. BTW It uses HTML::TokeParser, not HTML::Parser directly, but the former gets all its arguments from the callback calls in the latter, so it is affected, too.

    Let me take a look at the Exporter docs for require_version():

    The Exporter module will convert an attempt to import a number from a module into a call to $module_name->require_version($value). This can be used to validate that the version of the module being used is greater than or equal to the required version.

    The Exporter module supplies a default require_version method which checks the value of $VERSION in the exporting module.

    What's not clear? It means that in case of your call,
    use MIME::Types 1.005;
    this will construct a call:
    MIME::Types->require_version(1.005);
    for which either the module has provided a method, which does the checking itself, or, if this method doesn't exist (I guess that's the more common case, it's the case for MIME::Types) this will call a fallback method through inheritance, which compares the argument given to the variable $VERSION of the package, in this case, to $MIME::Types::VERSION; and croak if the module is not new enough.
Re: Module Version Checking - Checking Multiple Installations of the Module
by theorbtwo (Prior) on Jan 19, 2003 at 09:36 UTC

    use does a require, which finds the first occourance of the module in @INC, and then calls the module's import, passing it the approps args. The import can then do whatever it wants.

    In other words, the '1.005' isn't magical here, it just gets passed to Mime::Types::import, which can do with it what it will. I don't think require_version has anything in to to keep looking if $Mime::Types::VERSION is too low.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Module Version Checking - Checking Multiple Installations of the Module
by Pardus (Pilgrim) on Jan 19, 2003 at 14:10 UTC
    Im afraid not,

    Exporter just calls UNIVERSAL->VERSION

    quoting perldoc UNIVERSAL:
    "VERSION" will return the value of the variable $VERSION in the package the object is blessed into. If "REQUIRE" is given then it will do a comparison and die if the package version is not greater than or equal to "REQUIRE".

    So it won't search any further, it just dies.
    + Memento mori

    --
    Jaap Karssenberg || Pardus Larus <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
Re: Module Version Checking - Checking Multiple Installations of the Module
by John M. Dlugosz (Monsignor) on Jan 20, 2003 at 16:23 UTC
    As other replies have already detailed, it loads the first one it finds.

    You might look at Exporter::VA, which is my solution to having multiple versions. It has them all in the single file, though, rather than multiple copies of the file name along different paths. You could make a proxy module, say Types_versioned, that looks at the version number passed, looks for all copies for Types, and loads the right one.

    —John