'result'
là tuyên bố cuối cùng của khối { say "2 seconds are over!"; 'result' }
. Trong ngôn ngữ Perl, dấu chấm phẩy (không phải dòng mới) xác định kết thúc của hầu hết các câu lệnh.
Trong mã này:
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)
Dòng thứ hai có thể được viết lại như sau:
my $after = $timer.then({
say "2 seconds are over!";
'result'
}
); # 'result' is what the block returns
Đó dấu chấm phẩy chỉ đơn giản là kết thúc báo cáo kết quả say "2 seconds are over!"
.
Bên ngoài của một khối, dòng này
say "2 seconds are over!"; 'result';
thực sự là hai câu lệnh:
say "2 seconds are over!"; # normal statement
'result'; # useless statement in this context (hence the warning)
Đưa nhiều câu lệnh trong một dòng hiếm khi thay đổi cách họ cư xử:
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!
Nguồn
2016-01-27 00:32:31
Lệnh ' {…} 'Là một khối trống, tương tự như một chương trình con. –
Cảm ơn bạn đã có ý nghĩa với tôi – smith