How to Specify Which Version of Perl to Use on Centos

how can I install another version of perl on top of the default version on Centos

Use perlbrew, it allows you to manage multiple perl installations in your $HOME directory, completely isolated each of them.

Example:

%> sudo cpan App::perlbrew
%> perlbrew init

%> perlbrew install perl-5.16.3
%> perlbrew switch perl-5.16.3

And now you have perl 5.16.3 in your PATH:

%> perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for ...

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

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).

Is it possible to have two different Perl versions?

I'll try to articulate an answer that addresses all your requests, HTH:

  • I wouldn't touch system Perl, because other system software might depend on a particular version. If you need a different version of Perl 5 for development, it's arguably better to install it in a controlled, isolated environment

  • To do so you can use tools like Perlbrew: with Perlbrew you can install distinct versions of Perl 5, and you'll be able to switch back and forth according to your development needs

  • You say you want to "update to Perl 6". Perhaps you noticed I wrote Perl 5 in the previous paragraphs. Perl 6 is not just a new version of Perl, but a profound evolution of the language: it stems from the same core principles, but it is quite different from its older sibling. Code written in Perl 5 won't run in Perl 6, for example

To sum up:

  • You can have both Perl 5 and Perl 6 on your system, but they are distinct languages
  • You can have multiple versions of Perl 5 using Perlbrew
  • I would suggest not to tamper with the version of Perl distributed with the OS

How to specify Perl version for apache2 mod_cgi?

When using plain CGIs, the OS (through the #! line) is responsible for picking the interpreter. As we have established, the shebang was pointing to the wrong version of the interpreter.

The easiest way is to edit it. If you need the same script to work with two different perl versions depending on the call context, you can use something like #!/usr/bin/env perl in the shebang to have it respect the environment, then change the PATH variable for apache.

If you cannot edit the shebang, there is the option of going through mod_perl (make sure you compile it against the desired version) if you don't mind using this specific version for the whole webserver.

Another way would be to run the webserver within some kind of container (chroot, lxc, docker, or just using mount namespaces) so that your /usr/bin/perl points to some other version of the interpreter.

How should I install more than one version of Perl?

I install all of my perls completely in their own directory so they don't share anything with any other perl. To do that, you just tell the Configure script where to install everything. I like /usr/local/perls:

 % ./Configure -des -Dprefix=/usr/local/perls/perl-5.x.y

When I do that for multiple versions, I get a directory that has separate installations.


% ls -1 /usr/local/perls
perl-5.10.0
perl-5.10.1
perl-5.6.2
perl-5.8.8

They all have their own bin and lib directories:


% ls -1 /usr/local/perls/perl-5.10.0
bin
lib
man

Most of the common tools will figure out what to do if you call them with different perls:

/usr/local/perls/perl-5.10.0/bin/perl /usr/local/bin/cpan

However, you can take the perl you want to use the most and put it first in your path. I just make a symlink to /usr/local/bin/perl, but you can add directories to PATH as well.

The perlbrew does a lot of this for you and moves symlinks around to make one of them the default perl. I don't use it though because it doesn't make life easier for me. That's up to you decide on your own though.

Perl's rpm version differs from perl -v

Try running which perl.

You probably have multiple installed, and your path is determining which one you get.

A fairly common split is to have a /usr/bin/perl which is your 'system' perl and installed via your package manager, and have another, newer perl installed by downloading and building it into /usr/local/bin.



Related Topics



Leave a reply



Submit