Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be able to use the new 5.12 package version syntax without specifying the required Perl version?

Tags:

perl

I am confused about the need for the use VERSION syntax and the package NAME VERSION syntax introduced in Perl 5.12. I expected to have to specify both:

use v5.12;

package MyPackage 0.01;

and any examples I have seen look like this. However, by accident (i.e. copying & pasting without paying attention) I found that this new package version syntax worked when I had use 5.010 or even no version specified.

The underlying Perl installation is ActiveState 5.14, so in this case is the interpreter version the only requirement? I was under the impression that new features always required the use VERSION syntax, to assist backwards compatibility. Are there exceptions?

like image 313
Neil Hughes Avatar asked May 22 '13 12:05

Neil Hughes


2 Answers

Not every new feature is a feature.

The use VERSION syntax does this:

  • throw an error when the current perl version is too low.
  • import the appropriate set of features from the feature pragma. The perldoc page of my version of feature lists:

    bundle    features included
    --------- -----------------
    :default  array_base
    
    :5.10     say state switch array_base
    
    :5.12     say state switch unicode_strings array_base
    
    :5.14     say state switch unicode_strings array_base
    
    :5.16     say state switch unicode_strings
               unicode_eval evalbytes current_sub fc
    

    (actually, array_base was introduced in 5.16, but was added to the previous bundles for back-compat).

  • since 5.11.0, the strict pragma is activated in the current scope.

Some modifications are so backwards-compatible that there is no need to provide a way to deactivate them (this is what the feature pragma is about). The use MODULE VERSION syntax is largely backwards-compatible. Because of the missing comma between the version and import list in

use MODULE VERSION LIST;

this isn't an issue (i.e. this syntax was formerly illegal). When the LIST is missing, the import method would receive the version number on perls that don't recognize this syntax. The Exporter module handles this case, and checks the module version. So all modules that provide the import via Exporter (the majority) are safe.

like image 195
amon Avatar answered Sep 29 '22 11:09

amon


The use VERSION gives you all the stuff you could turn on explicitly with use feature, and complain if your current version is lower than what you want. See this doc: http://perldoc.perl.org/functions/use.html

An exception is raised if VERSION is greater than the version of the current Perl interpreter; Perl will not attempt to parse the rest of the file.

[..]

use VERSION also enables all features available in the requested version as defined by the feature pragma, disabling any features not in the requested version's feature bundle. See feature. Similarly, if the specified Perl version is greater than or equal to 5.11.0, strictures are enabled lexically as with use strict . Any explicit use of use strict or no strict overrides use VERSION , even if it comes before it. In both cases, the feature.pm and strict.pm files are not actually loaded.

Stuff that just changes, like behaviour of certain functions, regex modifiers and other stuff that is described in perldelta has nothing to do with this. You cannot force it to go back to behaviour of a previous version because that's not in your perl interpreter.


An example that occurs to me where we had this issue in production is the /r modifier for regular expressions. That was introduced in Perl 5.14.0. If you use it on 5.12 it will give a syntax error. As soon as it is run on 5.14 it will work, even with a program like this:

#!/usr/bin/perl
use strict; use warnings;
my $foo = 'foobar';
print $foo =~ s/foo/oof/r;
like image 36
simbabque Avatar answered Sep 29 '22 11:09

simbabque