我正在努力学习Perl。我编写了一个脚本,然后尝试找出运行该脚本所需的Perl的最低版本。
我被告知Perl附带了一个名为perlver
的程序,该程序将根据语法和明确说明的内容确定所需的最低版本。
我有以下代码:
#!/usr/bin/perl
use utf8;
use strict;
use autodie;
use warnings;
use diagnostics;
say "Hey";
exit 0;
但是当我通过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] $
它表示基于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.
该错误表明say
中未包含v5.8.0
。那么如何才能准确找出运行脚本所需的Perl最低版本?
或者可能有一个完整的Perison函数列表,我可以通过它来解析自己以获得最低版本吗?
答案 0 :(得分:5)
你可能会感到困惑的是,例如,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'
,以防止启用所有功能,无论您是否要使用它们,从而污染您的命名空间
答案 1 :(得分:4)
您的程序在任何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.
您也可以使用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.
首先编写一个有效的程序。
另一方面,假设您的计划包括以下内容:
use feature qw( say );
say "Hey";
此特定程序需要5.10,perlver
正确识别需要5.10。
------------------------------------
| 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 |
------------------------------------