TheAmigo TheAmigo - 1 month ago 7
Perl Question

Perl: specify minimum version in env var?

I'm looking for a shorter way to write simple one-offs like this:

perl -e 'use 5.016;say 4.3%3'


or

perl -e 'print 4.3%3 ."\n"'


I was hoping for an environment variable I could set that would be the equivalent of
use 5.016
so I could just write:

perl -e 'say 4.3%3'


Perhaps I've overlooked something in the documentation?

Answer
perl -E 'say ...';

This is like -e but turns on all features (which say is one of). From perldoc/perlrun:

  • -e commandline

    may be used to enter one line of program. If -e is given, Perl will not look for a filename in the argument list. [...]

  • -E commandline

    behaves just like -e, except that it implicitly enables all optional features (in the main compilation unit). See feature.


To be honest: my answer doesn't exactly answer your original question. You asked how to use/require a minimum Perl version. For that the solution given in @ThisSuitIsBlackNot's comment fits better:

perl -M5.016 -e 'say ...'

This turns on all features that came with Perl 5.16 and at the same time complains if your Perl version is less than 5.16. Look here and here for a when-came-what table.

The -E solution blindly turns on all features of your current Perl's version. You can decide which one to use, depending on your intention.


You also asked for an environment variable. There's indeed one:

export PERL5OPT='-Mstrict -Mwarnings -M5.016'

Switches in this variable are treated as if they were on every Perl command line.

Comments