3

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 ?

Kasper
  • 685
  • 2
  • 11
  • 30
  • Try using [`$^V`](http://perldoc.perl.org/perlvar.html) – Hunter McMillen May 08 '14 at 20:14
  • 2
    Put `use 5.010001` into the script that needs to run in the newer version. – choroba May 08 '14 at 20:45
  • 1
    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) .... – Kasper May 08 '14 at 20:52
  • What does it mean `to switch`? – mpapec May 08 '14 at 20:57
  • 1
    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 – Kasper May 08 '14 at 21:23
  • Why don't you run everything under Perl 5.10? – Borodin May 09 '14 at 01:17
  • How are you calling the 'another script'? What syntax do you use? backticks, system-call or something else?? – lexu May 09 '14 at 05:42

2 Answers2

7

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

Miller
  • 34,962
  • 4
  • 39
  • 60
  • 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. – Kasper May 08 '14 at 20:31
1

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.

marc-medley
  • 8,931
  • 5
  • 60
  • 66
Armali
  • 18,255
  • 14
  • 57
  • 171
  • One of the problems with this is that you don't know if `perl` is the same `perl` that you are running. For that, you would use `$^X` to get the path to the current `perl`. But, you don't need to run an external command because the `$^V` variable gives you this info. – brian d foy May 30 '23 at 13:44
  • The querist doesn't want to _know if `perl` is the same perl that you are running._ On the contrary, he wants to run another version. – Armali May 31 '23 at 11:33