Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

VERSION format & compare

by perl-diddler (Chaplain)
on Sep 19, 2017 at 21:48 UTC ( #1199696=perlquestion: print w/replies, xml ) Need Help??

perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to make sure a changed library mod was included, so I bumped the version. My mod:
package tlib; our $VERSION='0.0.3';
The prog:
#!/usr/bin/perl use warnings; use strict; use tlib '0.0.4';
I get no error. I vaguely remember that I was told that as long as the version strings were of the same format -- like a quoted literal, or a 'v' string, they'd compare correctly (I thought).

In fact, the problem seems to lie in the 'use-ing' prog. The version check works whether the module has quotes (or not), but the version check is ignored if the 'using' prog has a version in quotes in the same format as in the module.

Why doesn't this work? I.e. if a prog that is using a module uses the same version format as is used in the module, why doesn't compare happen correctly?

Does anyone think it would be worth asking for a "enhancement" on version checks to allow a quoted version string to work the same as if it was unquoted (or if it is in the same format used in the module)?

Replies are listed 'Best First'.
Re: VERSION format & compare
by haukex (Archbishop) on Sep 20, 2017 at 08:18 UTC

    This might be of interest:

    Foo->VERSION(...)Foo->import(...)
    use Foo-()
    use Foo ()--
    use Foo 'list'-('list')
    use Foo 1.23('1.23')()
    use Foo 1.23 ()('1.23')-
    use Foo 1.23 'list'('1.23')('list')
    use Foo 1.23, 'list'-('1.23','list')
    use Foo 1.23 , 'list'syntax error
    use Foo '1.23'-('1.23')
    use Foo 1.2.3(1.2.3)()
    use Foo 1.2.3 ()(1.2.3)-
    use Foo 1.2.3 'list'(1.2.3)('list')
    use Foo 1.2.3, 'list'-(1.2.3,'list')
    use Foo 1.2.3 , 'list'syntax error
    use Foo '1.2.3'-('1.2.3')
    use Foo '1.2.3' ()syntax error
    use Foo '1.2.3' 'list'syntax error
    use Foo '1.2.3', 'list'-('1.2.3','list')
    use Foo '1.2.3' , 'list'-('1.2.3','list')

    Note that (1.2.3) is a list of one v-string, as opposed to ('1.2.3'), a list of one regular string. Also, I was a bit surprised myself about use Foo 1.23, 'list' vs. use Foo 1.23 , 'list' (with the space before the comma) throwing an error as I hadn't encountered that case so far, but I guess it goes to show that use gets special parsing by Perl.

    Code used to generate the table follows:

    Update: For the sake of completeness, use Module VERSION LIST (where both VERSION and LIST are optional) is equivalent to:

    BEGIN {
      require Module;
      Module->VERSION( VERSION );  # if VERSION was specified
      Module->import( LIST );      # *except* when LIST is "()"
    }
    

    And use VERSION; requires a minimum version of Perl, enables the feature bundle for that version and strict if the version is 5.12 or greater. Also, if Module doesn't have a sub import, no error is raised. All packages inherit a method VERSION from UNIVERSAL, which apparently is defined in terms of version (a core module as of Perl 5.10, although I can't seem to find it on perldoc.perl.org, which I assume is a bug there).

Re: VERSION format & compare
by ikegami (Patriarch) on Sep 20, 2017 at 00:37 UTC

    The version can't be provided as a string. You ended up using

    use Module LIST;

    instead of

    use Module VERSION;
      That's dependent on the Module and its 'import' routine.

      If I use an 'import' routine that looks for a version number (regardless of whether it has been stringified or not) it can compare them as version numbers.

      As an example, download 'Xporter' from cpan and place it in the module, and then call the module -- you'll see that it flags an error.

      You could argue that version strings should have some other use, but I'd much prefer something that looks like a version string be given preference to being interpreted as a version string.

      I've yet to see any module accept a version-looking string as an argument. Not to say one couldn't exist, but it breaks the 'least astonishment' design principle, for the same expression to be taken as a version number in the module, but not in the useing module.

      Obviously someone felt differently when designing the versions, but do you know of any modules that use a version looking string as an argument in a use module statement?

      Looking at the link of anon-monk, I see the example using single quotes in the $VERSION statement, a recommendation against using v-strings and a use of a bare version number with 'use'.

      I don't see why it is that way.

Re: VERSION format & compare
by Anonymous Monk on Sep 19, 2017 at 22:11 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-08 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?