How to check perl version

13,471

Solution 1

The first 3 decimals are the subversion, and second 3 decimals are revision. Therefore use

if ($] >= 5.010001)
{
    ## We're all good.  Greater than 5.10.1
}

I primarily use 5.18.2, and therefore my $] equals 5.018002.

For alternative methods check out perlvar $PERL_VERSION or $^V and use VERSION

Solution 2

how can I check that the new perl version is >= 5.10.1 ?

You gave an answer in your question:

   my $perl_cmd = "perl --version";
   my $perl_str=`$perl_cmd`;
   print "PERL VERSION = " . $perl_str;   ## this clearly print 5.10.1

Since your change can only alter the version of newly started perl instances, you have to run a new perl to check, as you did above.

Share:
13,471
Kasper
Author by

Kasper

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand". M. Fowler

Updated on June 04, 2022

Comments

  • Kasper
    Kasper almost 2 years

    the default Perl version installed on my machine is the 5.8.7. When I run my script internally switch to another perl version (v5.10.1) doing this:

      my $perl_5_10 = "/opt/perl_5.10.1/bin";
      $ENV{'PATH'}  = $perl_5_10 ":" . $ENV{'PATH'};
    

    Now, I have to check the perl version, and I do:

       ## PERL: need perl version >= 5.10!
       if ($] < 5.010000)
       {
         ## VERSION ERROR!
       }
    
       my $perl_cmd = "perl --version";
       my $perl_str=`$perl_cmd`;
       print "PERL VERSION = " . $perl_str;   ## this clearly print 5.10.1
    

    it return error as the version used is 5.8.7 and that's pretty normal as I ran my script with that version. But my problem is:

    how can I check that the new perl version is >= 5.10.1 ?

    • Hunter McMillen
      Hunter McMillen almost 10 years
      Try using $^V
    • choroba
      choroba almost 10 years
      Put use 5.010001 into the script that needs to run in the newer version.
    • Kasper
      Kasper almost 10 years
      The main script I know will start with the version 5.8 but after being launched I switch to 5.10. At thet point I have to check if I switched correctly (or the new version is available) ....
    • mpapec
      mpapec almost 10 years
      What does it mean to switch?
    • Kasper
      Kasper almost 10 years
      That my script is launched with a perl version, then I add to the env variable another perl version (see code) 5.10 and then from this script I run another script. But first need to check that the new perl version is available ... That's all
    • Borodin
      Borodin almost 10 years
      Why don't you run everything under Perl 5.10?
    • lexu
      lexu almost 10 years
      How are you calling the 'another script'? What syntax do you use? backticks, system-call or something else??
  • Kasper
    Kasper almost 10 years
    Mmmh, my problem is that i started my script with a perl version (5.8) and then set the env var to perl version 5.10 but will be valid just for the perl script I run (into my script) after that ... So I think my unique chance is to run perl -version and parse it.