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

Can Anyone Please explain what is the use of below statement.
$VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf "%d."." +%02d" x $#r, @r };
I have seen it is used in several places.

Replies are listed 'Best First'.
Re: Perl usage (version number)
by LanX (Saint) on Nov 22, 2015 at 13:53 UTC
    Looks like someone is transforming one version number format to another one with 2 (shouldn't it be 3?) digits per "dimension".

    Run it and show us the result please.

    I suppose the revision string is auto generated from outside perl.

    Wouldn't be my favorite way of doing it.

    Updates
    • Please use code tags
    • haven't tried it but this code looks broken for more than 2 dimensions (missing separator)¹
    • looks like cargo cult, one hacker copied it without thought from another
    update

    ¹) There are various version number formats, this reflects one common approach to create a float number.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      from perlmod (v5.14)
      # set the version for version checking $VERSION = 1.00; # if using RCS/CVS, this may be preferred $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d ++)/g;

      compare CVS revisions

      So its a way to generate a module's $VERSION number from the version control system's revision number.

      ...well...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Ah, yes, right... That shook loose some ancient information, from back when I was using CVS.

        As I recall, CVS has some magic such that $Revision nnn$ will be automatically updated, replacing nnn will the current CVS revision number each time you check the file in.

        The rest of the code is to extract the number from the string $Revision nnn$ and reformat it so that each number (after the first) is displayed as two digits. So $Revision 1.2.3.4.5$ becomes 1.02.03.04.05.

Re: Perl usage
by Anonymous Monk on Nov 22, 2015 at 17:01 UTC
    my @matches = 'Revision 1.4 ' =~ /\d+/g; my $format = '%d.'; $format .= '%02d' x (scalar @matches - 1); my $version = sprintf $format, @matches; $VERSION = $version;
    Is it more understandable?