Promise 对象内外的分号
semicolon inside and outside Promise object
当我运行以下代码时:
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
;
里面的then
有什么作用,为什么我写
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
喜欢第一个例子吗?
'result'
是块 { say "2 seconds are over!"; 'result' }
的最后一条语句。在 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
该分号只是结束语句 say "2 seconds are over!"
。
块外,此行
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!
当我运行以下代码时:
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
;
里面的then
有什么作用,为什么我写
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
喜欢第一个例子吗?
'result'
是块 { say "2 seconds are over!"; 'result' }
的最后一条语句。在 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
该分号只是结束语句 say "2 seconds are over!"
。
块外,此行
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!