tags:

views:

132

answers:

3

To ensure a script has at least version X of perl, you can do the following

require 5.6.8;

What is the best way of checking that a version is not too recent? (i.e. version 5.8.x if fine, but 5.9 or 5.10 are not ok).

+15  A: 

This code will die if the version of Perl is greater than 5.8.9:

die "woah, that is a little too new" unless $] <= 5.008009;

You can read more about $] in perldoc perlvar.

Chas. Owens
+4  A: 

You can use the special $^V variable to check the version. From perldoc perlvar:

$^V

The revision, version, and subversion of the Perl interpreter, represented as a 
version object.

This variable first appeared in perl 5.6.0; earlier versions of perl will see an    
undefined value. Before perl 5.10.0 $^V was represented as a v-string.

You can use $^V in a string comparison, e.g.

if ( $^V lt 'v5.10.0' )

If you may be running on a perl earlier than 5.6.0, you'll need to use $] which returns a simple integer.

friedo
I don't think that string comparison will work without version.pm (which many versions of 5.6+ perl don't necessarily have). Did you mean ($^V lt v5.10.0)? I don't see any reason to favor $^V over $].
ysth
If you're trying to use old perls, this isn't the way to go.
brian d foy
A: 

The simplest solution would be to do this:

no 5.010;
Leon Timmermans
Doesn't really work: try `no 5.011` and you'll get an error that `feature bundle "5.11.0" is not supported by Perl 5.10.0`. Rather broken....
derobert