我正在努力学习Perl.我编写了一个脚本,然后尝试找出运行脚本所需的Perl的最低版本.
我被告知Perl附带了一个程序,该程序perlver将根据语法和明确说明的内容确定所需的最低版本.
我有以下代码:
#!/usr/bin/perl
use utf8;
use strict;
use autodie;
use warnings;
use diagnostics;
say "Hey";
exit 0;
Run Code Online (Sandbox Code Playgroud)
但是当我运行脚本时perlver,我得到以下输出:
[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] $
Run Code Online (Sandbox Code Playgroud)
它说基于sytax的最低版本是v5.8.0.但是当我添加use v5.8.0到脚本时,我收到一个错误:
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.
Run Code Online (Sandbox Code Playgroud)
该错误表明say不包括在内v5.8.0.那么如何才能准确找出运行脚本所需的最低Perl版本?
或者可能有一个由verison编号列出的Perl函数的完整列表,我可以解析自己以获得最低版本?
你可能会感到困惑的是,例如,use 5.010(或use v5.10)做了两件事
它确保当前Perl解释器的版本至少为5.10.那是什么require 5.010呢
它具有相同的功能use feature ':5.10',可以启用该版本中的所有功能
这意味着如果您添加use 5.010到您的程序,它将启用该say功能等功能
但没有任何一个use feature 'say'或use 5.010你的程序甚至不会编译,所以perlver会给你错误的答案
我建议使用require 5.010和use feature 'say'单独使用以防止启用所有功能,无论您是否要使用它们,从而污染您的命名空间