A semicolon inside and outside a Promise object

When I run the following code:

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

I get

 2 seconds are over! result 

What is the role ; inside then and why if i write

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

get the following error?

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

and not:

 2 seconds are over! result 

how is the first example?

+5
source share
1 answer

'result' is the last block statement { say "2 seconds are over!"; 'result' } { say "2 seconds are over!"; 'result' } . In Perl, semicolons (not newlines) define the ends of most statements.

In this code:

 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) 

The second line can be rewritten in this way:

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

This semicolon just ends the statement say "2 seconds are over!" .

Outside the block, this line

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

there are really two operators:

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

Including several statements on the same line rarely changes their behavior:

 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! 
+6
source

Source: https://habr.com/ru/post/1241534/


All Articles