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

Hi.
I'm trying to get perl's version from a script, to show in a html page, like "v5.6.1".
I have tried the var $^V, using s/(.)(.)(.)/ord($1).".".ord($2).".".ord($3)/e, but something "invisible" returns and changes the encoding from the page, and destroys my latin1 characteres.
TIA for any help.

Replies are listed 'Best First'.
Re: Obtaining perl's version from script
by sauoq (Abbot) on Nov 12, 2002 at 01:27 UTC

    There are several ways to handle the version depending on what you want to do with it. I like to break it up into its components like so:

    my ($major, $minor, $patch) = unpack 'CCC', $^V;

    If you just want to print it, have a look at the docs for sprintf() and printf(). They support things like:

    my $version_string = sprintf "%vd", $^V;

    If you want to compare it you can do so directly using Perl's new "vector of ordinal" type strings:

    warn "Unsupported version\n" unless $^V ge v5.6.0;

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Obtaining perl's version from script
by Zaxo (Archbishop) on Nov 12, 2002 at 01:25 UTC

    The Camel (3ed.) suggests this may give what you want:

    my $perl_version = sprintf 'v%vd', $^V;

    After Compline,
    Zaxo

      $^V isn't going to work for versions prior to 5.6.0, so $] may be the better option if you can't guarantee how old a version of Perl you'll be running.

      $ perl5.00503 -le 'print $]' 5.00503 $ perl5.6.0 -le 'print $]' 5.006 $ perl5.6.1 -le 'print $]' 5.006001

      Or, if you want to "pretty print" it...

      $ perl5.00503 -le 'printf "v%d.%d.%d\n", $] =~ /^(\d+)\.(\d{3})(\d*)/' v5.5.3 $ perl5.6.0 -le 'printf "v%d.%d.%d\n", $] =~ /^(\d+)\.(\d{3})(\d*)/' v5.6.0 $ perl5.6.1 -le 'printf "v%d.%d.%d\n", $] =~ /^(\d+)\.(\d{3})(\d*)/' v5.6.1

          --k.


Re: Obtaining perl's version from script
by pg (Canon) on Nov 12, 2002 at 02:03 UTC
    I made a little modification to your s///, and it works now:
    s/./ord($&)/eg;