This will extract the cvs version number for the current file into a scalar.
$VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/);

Replies are listed 'Best First'.
Re: cvs revision extraction regex
by Chmrr (Vicar) on Apr 21, 2002 at 18:43 UTC

    I use the following (first seen, if I remember correctly, from one of merlyn's bits of code):

    $VERSION = (qw$Revision: 1.12 $ )[-1];

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      I used the same until I ran into a problem comparing versions between a single and double-digit minor version.

      my $new = 1.41; my $old = 1.5; print $new > $old ? $new : $old;

      I figured 'nothing a fancier sort can't fix, right?' until I realized it also broke use My::Module N.NN;, so thesedays I use a style I saw in one of Gisle Aas' modules...

      $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/);

      That way single digits get 0-padded (ie, 1.05 vs 1.5, although 1.1.1.1 gets turned into 1.01) and all is right with the world again. :)

          --k.


        It's so cool to have gotten such a response to this little snippet. :-)

        I certainly like Gisle's version (thanks kanji!). I might do something more like %03d instead of %02d since some of my files get revised that often, but otherwise that's the best thing I've seen.

        I changed the snippet itself to reflect this...