我該如何重寫這個以便info
在後臺運行,直到$aw
等於result
?在後臺運行一個子例程,直到出現一個條件
#!/usr/bin/env perl
use 5.12.0;
use warnings;
use Term::ReadLine;
my $term = Term::ReadLine->new('something');
$term->ornaments(0);
sub info {
# in the real script this runs some computations instead of the sleep
# and returns some information.
my ($time) = @_;
sleep $time;
return $time * 2;
}
my $value_returned_by_info = info(10); # run this in the background
my $aw;
$aw = $term->readline('User input: ');
if ($aw eq 'result') {
# if info() is still running in the background:
# wait until info() returns because "$value_returned_by_info" is needed.
say $value_returned_by_info;
}
else {
# if info() is still running in the background:
# let info() in the background because "$value_returned_by_info" is not needed here.
say $aw;
}
$aw = $term->readline('User input: ');
if ($aw eq 'result') {
# if info() is still running in the background:
# wait until info() returns because "$value_returned_by_info" is needed.
say $value_returned_by_info;
}
else {
# if info() is still running in the background:
# let info() in the background because "$value_returned_by_info" is not needed here.
say $aw;
}
$aw = $term->readline('User input: ');
if ($aw eq 'result') {
# if info() is still running in the background:
# wait until info() returns because "$value_returned_by_info" is needed.
say $value_returned_by_info;
}
else {
# if info() is still running in the background:
# let info() in the background because "$value_returned_by_info" is not needed here.
say $aw;
}
say "End";
我們需要你想要'info'做什麼,以及它如何與主程序交互的更多信息。 「信息」中的「睡眠」是否真的是睡眠,還是代表着「info」與主程序同時進行的工作? 'info'可以在單獨的進程中運行,還是需要運行在與'readline'調用運行相同的進程? 另外,如果主程序在'info'返回值之前調用'$ result'的值會發生什麼?主程序是否阻止或者'$ result'只是'undef'或者以前的值? – ErikR
睡眠在那裏代表工作。如果主程序在info返回之前調用$ result的值,那麼它必須等到info返回值。 –