Monday, 1 July 2013

Running a perl script with a particular version of Perl

If you want to use a perl script that only appeared in a particular version of perl (eg. the 'say' command, which appeared in perl 5.10), then you can specify inside the script that you can specify in the perl script that you want to use perl5.10 and that new features (such as 'say') should be enabled:

#!/usr/local/bin/perl
use 5.010;
say 'Hello';


Then you need to run the perl script with the correct version of perl for it to work:
% /software/perl-5.10.1/bin/perl -w my.pl
Hello

Note that if you don't use the correct version of perl, you'll get an error message:
% perl -w my.pl
Perl v5.10.0 required--this is only v5.8.8, stopped at my.pl line 3.
BEGIN failed--compilation aborted at my.pl line 3.


Note also that if you don't include the "use 5.010" in your script, it won't enable new features specific to 5.010 by default, and you'll get an error message because the 'say' command won't be recognised:

#!/usr/local/bin/perl
say 'Hello';


% /software/perl-5.10.1/bin/perl -w my.pl
Unquoted string "say" may clash with future reserved word at my.pl line 3.
String found where operator expected at my.pl line 3, near "say 'Hello'"
    (Do you need to predeclare say?)
syntax error at my.pl line 3, near "say 'Hello'"
Execution of my.pl aborted due to compilation errors.

No comments:

Post a Comment