[英]Why do I get a syntax error when I use “given”?
I'm trying to run the following code: 我正在尝试运行以下代码:
foreach my $k (keys %rec) {
#switch for watchlist figures
given ($k) { #line 93
# Code omitted
when ("p") { #positive breakout
if ($row{cls} > $rec{$k}) {
$email .= "positive";
} # line 104
}
when ("n") { #negative breakout
if ($row{cls} < $rec{$k}) { #line 107
But I get a syntax error: 但我得到一个语法错误:
syntax error at check_watch.pl line 93, near ") {"
syntax error at check_watch.pl line 104, near "}"
Unterminated <> operator at check_watch.pl line 107.
Why? 为什么?
given
and when
are only available when either use feature "switch";
given
和when
仅在use feature "switch";
时可用use feature "switch";
or use v5.10;
或use v5.10;
(or some later value) is in effect. (或某些后来的值)生效。 Place one of those lines at the top of your source file. 将其中一行放在源文件的顶部。
It is because it is not on by default. 这是因为它默认不启用。 Add the line: 添加行:
use 5.10.1;
to the code 到代码
Because pre-5.10 versions of Perl 5 don't have the given
or when
keywords, you're allowed to define custom functions with those names, which of course would have different syntax. 由于Perl 5之前的5.10版本没有given
或者when
关键字,因此您可以使用这些名称定义自定义函数,这当然会有不同的语法。 To avoid breaking backward-compatibility with programs that do that, given
and when
are only enabled if you specifically ask for them, by putting either 为了避免破坏与执行该操作的程序的向后兼容性,只有在您特别要求时才启用given
和when
,通过放置其中任何一个
use 5.010;
or 要么
use feature 'switch';
at the top of a lexical scope you want to use the keywords in. In addition, the semantics keep changing. 在词法范围的顶部,你想使用关键字。此外,语义不断变化。 For example, given
was originally designed to use lexical $_
by default, but lexical $_
turns out to be a really poor fit for Perl 5, an issue they're still revising. 例如, given
最初设计为默认使用词汇$_
,但词汇$_
对于Perl 5来说是一个非常不合适的问题,他们仍在修改这个问题。 At some point, given
stopped lexicalizing $_
, but of course that's a backward-incompatible change. 在某些时候, given
停止词汇化$_
,但当然这是一个向后不兼容的变化。 when
is (mainly) designed to use ~~
, but that operator had very complicated semantics in 5.10; when
(主要)设计使用~~
,但该操作符在5.10中具有非常复杂的语义; they've been revised once and there are plans to revise them again. 它们已被修改过一次,并且有计划再次修改它们。 (This is why the Perl 5 developers have decided to just mark all new features as 'experimental' when they're first included in a development release). (这就是为什么Perl 5开发人员决定在将它们首次包含在开发版本中时将所有新功能标记为“实验性”的原因)。
Because they are experimental, to use them without warnings you also need to include: 因为它们是实验性的,要在没有警告的情况下使用它们,您还需要包括:
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
or add a 5.18 dependency and use 或添加5.18依赖项并使用
no warnings 'experimental::smartmatch';
, or add a dependency on the experimental
CPAN module and use ,或添加对experimental
CPAN模块的依赖和使用
use experimental 'smartmatch';
. 。
Now, on the other hand, the Perl 5 developers (of course) need people to use given
, when
, and ~~
in non-production-critical (or thoroughly unit-tested!) code and give them feedback. 现在,另一方面,Perl 5开发人员(当然)需要人们在非生产关键(或完全经过单元测试的!)代码中使用given
, when
和~~
并给予他们反馈。 So definitely do use them if you can. 如果可以的话,一定要使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.