Reputation: 2385
I'm trying to learn Perl. I write a script and then try to find out what the minimum version of Perl that's needed to run the script.
I was told Perl comes with a program called perlver
which will determine the minimum version needed based on both syntax and by what's explicitly stated.
I have the following code:
#!/usr/bin/perl
use utf8;
use strict;
use autodie;
use warnings;
use diagnostics;
say "Hey";
exit 0;
But when I run the script through perlver
, I get the following output:
[me@here PERL] $ perlver ex6-1
------------------------------------
| file | explicit | syntax | external |
| ------------------------------------ |
| ex6-1 | ~ | v5.8.0 | n/a |
| ------------------------------------ |
| Minimum explicit version : ~ |
| Minimum syntax version : v5.8.0 |
| Minimum version of perl : v5.8.0 |
------------------------------------
[me@here PERL] $
It says that the minimum version based on sytax is v5.8.0
. But when I add use v5.8.0
to the script, I get an error:
String found where operator expected at ./ex6-1 line 16, near "say "Hey"" (#1)
(S syntax) The Perl lexer knows whether to expect a term or an operator.
If it sees what it knows to be a term when it was expecting to see an
operator, it gives you this warning. Usually it indicates that an
operator or delimiter was omitted, such as a semicolon.
(Do you need to predeclare say?) (#2)
(S syntax) This is an educated guess made in conjunction with the message
"%s found where operator expected". It often means a subroutine or module
name is being referenced that hasn't been declared yet. This may be
because of ordering problems in your file, or because of a missing
"sub", "package", "require", or "use" statement. If you're referencing
something that isn't defined yet, you don't actually have to define the
subroutine or package before the current location. You can use an empty
"sub foo;" or "package FOO;" to enter a "forward" declaration.
syntax error at ./ex6-1 line 16, near "say "Hey""
Execution of ./ex6-1 aborted due to compilation errors (#3)
(F) Probably means you had a syntax error. Common reasons include:
A keyword is misspelled.
A semicolon is missing.
A comma is missing.
An opening or closing parenthesis is missing.
An opening or closing brace is missing.
A closing quote is missing.
Often there will be another error message associated with the syntax
error giving more information. (Sometimes it helps to turn on -w.)
The error message itself often tells you where it was in the line when
it decided to give up. Sometimes the actual error is several tokens
before this, because Perl is good at understanding random input.
Occasionally the line number may be misleading, and once in a blue moon
the only way to figure out what's triggering the error is to call
perl -c repeatedly, chopping away half the program each time to see
if the error went away. Sort of the cybernetic version of 20 questions.
Uncaught exception from user code:
syntax error at ./ex6-1 line 16, near "say "Hey""
Execution of ./ex6-1 aborted due to compilation errors.
The error suggests that say
isn't included in the v5.8.0
. So how can I accurately find out the minimum required version of Perl needed to run a script?
Or might there be a complete list of Perl functions listed by verison number that I can parse myself to get the minimum version?
Upvotes: 0
Views: 177
Reputation: 126722
What is probably confusing you is that, for instance, use 5.010
(or use v5.10
) does two things
It ensures that the version of the current Perl interpreter is at least 5.10. That's what require 5.010
does
It does the equivalent of use feature ':5.10'
, which enables all features available in that version
That means if you add use 5.010
to your program it will enable, amongst other things, the say
feature
But without either use feature 'say'
or use 5.010
your program won't even compile, so perlver
will give you the wrong answer
I recommend using require 5.010
and use feature 'say'
separately so as to prevent all of the features being enabled whether you want to use them or not, and so polluting your namespace
Upvotes: 5
Reputation: 385917
Your program isn't valid in any version of Perl.
$ perl5.24.0 -e'use utf8; use strict; use autodie; use warnings; say "Hey"; exit 0;'
String found where operator expected at -e line 1, near "say "Hey""
(Do you need to predeclare say?)
syntax error at -e line 1, near "say "Hey""
Execution of -e aborted due to compilation errors.
You might as well have used foo "Hey";
.
$ perl5.24.0 -e'use utf8; use strict; use autodie; use warnings; foo "Hey"; exit 0;'
String found where operator expected at -e line 1, near "foo "Hey""
(Do you need to predeclare foo?)
syntax error at -e line 1, near "foo "Hey""
Execution of -e aborted due to compilation errors.
Start by writing a program that works.
On the other hand, let's say your program included the following:
use feature qw( say );
say "Hey";
This particular program requires 5.10, and perlver
correctly identifies that 5.10 is required.
------------------------------------
| file | explicit | syntax | external |
| ------------------------------------ |
| a.pl | ~ | v5.10.0 | n/a |
| ------------------------------------ |
| Minimum explicit version : ~ |
| Minimum syntax version : v5.10.0 |
| Minimum version of perl : v5.10.0 |
------------------------------------
Upvotes: 5