Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify in the script to only use a specific version of perl?

Tags:

perl

I have a perl script written for version 5.6.1 and which has dependencies on Oracle packages, DBI packages and more stuff. Everything is correctly installed and works.

Recently perl version 5.8.4 got installed and because those dependencies are not correctly set so the script fails.

'perl' command points to /program/perl_v5.8.4/bin/perl -- latest version

So, when I have to run my perl script I have to manually specify in command prompt

 /program/perl_v5.6.1/bin/perl scriptName.pl

I tried adding following lines in script:

 /program/perl_v5.6.1/bin/perl 
 use v5.6.1;

But, this means script has to take Perl version > 5.6.1

I found couple of related question which suggested:

  1. To export path. But, I want the script for all the users without have to export path
  2. Specify version greater than. But, I want to use just one specific version for this script.
  3. use/require commands. Same issue as above.

My question: How to specify in the script to only use a specific version of perl?

like image 402
JoshMachine Avatar asked Jul 08 '12 21:07

JoshMachine


2 Answers

The problem is that the interpreter specified in the shebang line (#!/some/path/to/perl) is not used if perl script is called like this:

perl some_script.pl

... as the 'default' (to simplify) perl is chosen then. One should use the raw power of shebang instead, by executing the file itself:

./some_script.pl

Of course, that means this file should be made executable (with chmod a+x, for example).

like image 130
raina77ow Avatar answered Sep 20 '22 22:09

raina77ow


I have in my code:

our $LEVEL = '5.10.1';
our $BRACKETLEVEL = sprintf "%d.%03d%03d", split/\./, $LEVEL;

if ($] != $currentperl::BRACKETLEVEL)
{
    die sprintf "Must use perl %s, this is %vd!\n", $LEVEL, $^V;
}

These are actually two different modules, but that's the basic idea. I simply "use correctlevel" at the top of my script instead of use 5.10.1; and I get this die if a developer tries using the wrong level of perl for that product. It does not, however, do anything else that use 5.10.1; would do (enable strict, enable features like say, switch, etc.).

like image 25
Tanktalus Avatar answered Sep 17 '22 22:09

Tanktalus