1

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?

1 Answer 1

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

Be cautious when using the environment variable and sharing code: if you forget to tell your colleagues about it (because you set it months ago in your .bashrc), then some snippets might work for you but not for others.

Sign up to request clarification or add additional context in comments.

1 Comment

may want to add that in a script, one would have to use use feature 'say'; or use 5.10; or the like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.