3

Is there an easy way to get the version of python from a perl script. E.g. Get the equivalent of the version python -V. I need this to determine if I need to run python26 or just python on some of my linux boxes.

If there isn't an easy way, I plan to run the python -V then capture stdout and parse it.

James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • You could try to use exec() or system() to invoke python -V, not sure about how to redirect the output though –  Sep 25 '14 at 18:52

1 Answers1

5

You can execute any system command and capture STDOUT with qx:

use warnings;
use strict;

my $v = qx(python -V);
print $v;
toolic
  • 57,801
  • 17
  • 75
  • 117
  • 1
    I'll probably use this and something like `if (index($str, "2.6.") != -1) { ...; } ` to check after that. I'll accept your answer once the wait period is done. – James Oravec Sep 25 '14 at 18:56
  • 1
    A similar approach: `perl -E 'chomp( $v = qx{ python -c "import platform; print platform.python_version()" }); say $v'` -> 2.7.2 – Ashley Sep 25 '14 at 19:24
  • @VenomFangs or use regex `if ($str =~ /\Q2.6./) {..}` – mpapec Sep 25 '14 at 20:32