加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Promise对象内外的分号

发布时间:2020-12-16 06:26:44 所属栏目:大数据 来源:网络整理
导读:当我运行以下代码时: my $timer = Promise.in(2);my $after = $timer.then({ say "2 seconds are over!"; 'result' });say $after.result; # 2 seconds are over # result 我明白了 2 seconds are over!result 有什么作用;在那里,为什么如果我写 say "2 sec
当我运行以下代码时:

my $timer = Promise.in(2);
my $after = $timer.then({ say "2 seconds are over!"; 'result' });
say $after.result;  # 2 seconds are over
                    # result

我明白了

2 seconds are over!
result

有什么作用;在那里,为什么如果我写

say "2 seconds are over!"; 'result';

我收到以下错误?

WARNINGS:
Useless use of constant string "result" in sink context (line 1)
2 seconds are over!

并不是 :

2 seconds are over!
result

像第一个例子?

解决方法

‘结果’是块的最后一个声明{说“2秒结束!”; ‘结果’}.在Perl语言中,分号(不是换行符)决定了大多数语句的结尾.

在这段代码中:

my $timer = Promise.in(2);
my $after = $timer.then({ say "2 seconds are over!"; 'result' }); # 'result' is what the block returns
say $after.result;  # 2 seconds are over (printed by the say statement)
                    # result ('result' of the block that is stored in $after)

第二行可以改写:

my $after = $timer.then( {
                          say "2 seconds are over!";
                          'result'
                         }
); # 'result' is what the block returns

那个分号简单地结束了声明说“2秒结束了!”.

在一个街区之外,这条线

say "2 seconds are over!"; 'result';

真的是两个陈述:

say "2 seconds are over!";  # normal statement
'result';                   # useless statement in this context (hence the warning)

将多个语句放在一行中很少会改变它们的行为方式:

my $timer = Promise.in(2); my $after = $timer.then({ say "2 seconds are over!"; 'result' }); say $after.result; # Still behaves the same as the original code. ... Do not edit. This is intentionally crammed into one line!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读