简体   繁体   English

当我使用“given”时为什么会出现语法错误?

[英]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"; givenwhen仅在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 为了避免破坏与执行该操作的程序的向后兼容性,只有在您特别要求时才启用givenwhen ,通过放置其中任何一个

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开发人员(当然)需要人们在非生产关键(或完全经过单元测试的!)代码中使用givenwhen~~并给予他们反馈。 So definitely do use them if you can. 如果可以的话,一定要使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Perl中,为什么在尝试使用字符串eval时会出现语法错误? - In Perl, why do I get a syntax error when I try to use string eval? 当我尝试打印包含带有冒号的键的嵌套哈希时,为什么会出现语法错误? - Why do I get a syntax error when I try to print a nested hash that has keys containing colons? 尝试将值插入具有“索引”列的表时,为什么会出现SQL语法错误? - Why do I get a SQL syntax error when I try to insert values into a table with a column named “index”? 当我尝试为mips64交叉编译libperl时,为什么会出现语法错误? - Why do I get a syntax error when I try to cross compile libperl for mips64? 当我想打印到哈希键中的句柄时,为什么会出现语法错误? - Why do I get a syntax error when I want to print to a handle in a hash key? 当我使用Email :: MIME :: CreateHTML时,为什么在Email / MIME / CreateHTML.pm的第203行“ croak”附近必须显示正文”附近收到“语法错误”? - Why do I get 'syntax error at Email/MIME/CreateHTML.pm line 203, near “croak ”You must supply a body“”' when I use Email::MIME::CreateHTML? 为什么在尝试显示源时出现.perldb别名语法错误? - Why do I get .perldb alias syntax error when trying to display source? 为什么在此Perl代码中出现“&#39;$ rocks [&#39;附近的语法错误””? - Why do I get “Syntax error near '$rocks['” in this Perl code? 为什么在我的代码生成程序中出现此语法错误? - Why do I get this syntax error in my code generating program? 当我运行Perl单行代码时,为什么在意外的标记`(&#39;)附近出现“ -bash:语法错误”? - Why do I get “-bash: syntax error near unexpected token `('” when I run my Perl one-liner?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM