Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Programmatically comparing module version numbers

by Anonymous Monk
on Sep 29, 2008 at 17:27 UTC ( #714384=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a small Perl application, and I have a few questions regarding modules and their version numbers:

1) In order to check whether a module is installed, I currently do the following:
eval { require Term::Title; }; if ( $@ ) { warn the user that Term::Title was not found, so he'll be missing some feature in my application }
Is there a better way to detect whether the module is there?

Say I've changed the module, it has an error inside, and it won't load properly, but the module is there. Or maybe the user does not have file permissions to read the module file, but the file is. In this case, I would like the application to fail with the right error message, instead of just blindly reporting that the module is not there at all.

2) How do I reliably compare version numbers? I've seen that there are more than one version number formats, I'm not sure if a simple string or a simple numeric compare will do.

This is what I would like to achieve:

require Term::Title; if ( ! is_module_version_at_least( "Term::Title", "a.b" ) ) { print "I can use this module version, but it has known bugs and + you should upgrade it." }
That is, I would like some example code for routine is_module_version_at_least.

3) I'm finding the problem that some perl distributions do not ship with some modules by default, and others have older versions. I do not think that the end user should necessarily know how to install modules from CPAN or the like. You need different command-line incantations in order to install modules CPAN under Cygwin (no sudo available), under Debian or under Windows (maybe ActiveState's ppm). A particular application user may not necessarily have root privileges to install modules for all Perl users.

I would like to ship some CPAN module XXX together with my application. If the currently-installed perl has the same or newer version of XXX, I would like to use the already-installed versions. If not, I would like the application to fall-back to the version supplied with it.

How do I achieve that? Or maybe you think of a better approach to distribute applications and modules?

Many thanks, Ruben

Replies are listed 'Best First'.
Re: Programmatically comparing module version numbers
by ikegami (Pope) on Sep 29, 2008 at 19:32 UTC
    To error:
    use Module 4.56; # 4.50 adds foo() # 4.56 fixes bug in bar()
    To warn:
    use Module; if ( !eval { Module->VERSION(4.56); 1 } ) { warn(...); }
Re: Programmatically comparing module version numbers
by tod222 (Pilgrim) on Sep 29, 2008 at 23:08 UTC

    Regarding your third point, it appears that the Perl Archive Toolkit has a fallback option which allows the use of modules that are already installed but will fall back if needed and use a version included in the PAR file.

    I've also been thinking about module requirement issues for my application (still unreleased).

    I've decided to offer end users a number of choices:

    1. Standard CPAN release, for users who can install from CPAN.
    2. Prepackaged for certain Linux distributions (likely just Debian and Ubuntu at first).
    3. Binary executable produced with pp, the PAR Packager utility, for those without the knowledge or permissions needed for the first two options.
Re: Programmatically comparing module version numbers
by j1n3l0 (Friar) on Sep 30, 2008 at 10:54 UTC
    To compare VERSION numbers investigate the CPAN module version

    shell> perl -Mversion -le '$v1 = version->new("0.1.1"); $v2 = version- +>new("0.1002"); print $v1 > $v2 ? $v1 : $v2' # prints 0.1002

    Smoothie, smoothie, hundre prosent naturlig!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://714384]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2018-06-19 09:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.