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

One of the new features of Perl 5.6.0 is:
printf "Perl version: %vd\n", $^V; # prints 'Perl version 5.6.0'
In 5.6, printf and sprintf have now had the v flag added to their format specifications. Okay, so that example is for 5.6. How would you accomplish this with older versions of Perl 5?

Would it just be?:

join ".", map {ord} split //, $^V
But would this work across most versions of Perl prior to 5.6?

Replies are listed 'Best First'.
Re: Formatting Perl's version numbers
by hv (Prior) on Dec 08, 2004 at 11:26 UTC

    The portable way to handle versions is with the version module. In this case you just want the default stringification of the version object:

    zen% /opt/bleadperl/bin/perl -Mversion -wle 'print version->new($])' 5.9.2 zen% /opt/perl5.005_03/bin/perl -Mversion -wle 'print version->new($ +])' Unquoted string "version" may clash with future reserved word at -e +line 1. 5.5.30 zen%

    (That warning is cruft you can avoid by quoting the module name: print "version"->new($]).)

    Hugo

Re: Formatting Perl's version numbers
by Joost (Canon) on Dec 08, 2004 at 08:07 UTC
    The map { ord } split // construct should work in at least all perl 5 + versions, but remember that strings can contain characters of ord() > 255 in 5.6 + because of unicode support.

    Your code as presented will not work in versions prior to 5.6, because the $^V variable does not contain version information prior to 5.6. (See perl56delta).

    To get at the version info in a portable way, you probably need to use the $] variable. See perlvar