5

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

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
  • 3
    I'd be curious to know in which context this would be useful. Perl tends to go out of its way to preserve backward compatibility. – JB. Sep 04 '09 at 06:11
  • @JB A theoretical reason: later versions of Perl may have unexpected bugs. For instance, 5.10.0 had a memory leak related to qr//. In certain locked down environments an older version with known bugs is safer than a new version with unknown bugs. – Chas. Owens Sep 04 '09 at 06:15
  • 1
    In my case, I have a system which includes a bunch of compiled libraries which won't work on perl 5.10. – Matt Sheppard Sep 04 '09 at 06:18
  • +1 for thinking of system stability :) – DVK Sep 04 '09 at 16:24
  • Perl doesn't tend that hard to preserve backward compatibility. Features often disappear (like psuedohashes), change (like smart matching), or be binary incompatible. – brian d foy Sep 04 '09 at 16:30
  • Yup, I have this check in my code before I do some internal mucking around with pseudohashes: if ($] >= 5.009) { die "..." } (Not as much because I'm sure the code will break in 5.10, but that I haven't done adequate testing there to be absolutely confident, and my firm is still not wanting to upgrade all systems to 5.10.) – Ether Sep 04 '09 at 16:53
  • 3
    @chas That would seem to make the false assumption that once a bug is introduced it will always be there. – Schwern Sep 05 '09 at 06:59
  • If you are using compiled libraries, you may have to lock it down to a minor version. i.e. `5.6.0` .. `5.6.999` – Brad Gilbert Sep 08 '09 at 02:12

3 Answers3

23

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
  • 64,182
  • 22
  • 135
  • 226
8

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.

Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
friedo
  • 65,762
  • 16
  • 114
  • 184
  • 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 Sep 04 '09 at 07:31
  • 3
    If you're trying to use old perls, this isn't the way to go. – brian d foy Sep 04 '09 at 16:26
0

The simplest solution would be to do this:

no 5.010;
Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
  • 2
    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 Sep 06 '09 at 12:50