2012-11-18 17 views
0

我該如何重寫這個以便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"; 
+0

我們需要你想要'info'做什麼,以及它如何與主程序交互的更多信息。 「信息」中的「睡眠」是否真的是睡眠,還是代表着「info」與主程序同時進行的工作? 'info'可以在單獨的進程中運行,還是需要運行在與'readline'調用運行相同的進程? 另外,如果主程序在'info'返回值之前調用'$ result'的值會發生什麼?主程序是否阻止或者'$ result'只是'undef'或者以前的值? – ErikR

+0

睡眠在那裏代表工作。如果主程序在info返回之前調用$ result的值,那麼它必須等到info返回值。 –

回答

0

我同意user5402。提及在後臺運行和睡眠信息功能會引發很多問題。

我想知道,如果給定的輸入不正確,可能您正在尋找一種更爲簡潔的輸入重新提示方式。如果是這樣的話,那麼IO :: Prompter模塊可能適合你。

#!/usr/bin/env perl 

use 5.10.0; 
use strict; 
use warnings; 
use IO::Prompter; 

sub info { 
    my ($time) = @_; 
    sleep $time; 
    return $time * 2; 
} 

my $expect = info(10); 
my $aw; 

PROMPT: 
{ 
    $aw = IO::Prompter::prompt('Enter number', -i); 

    if ($aw eq $expect) { 

     say "$aw :)"; 
    } 
    else { 

     say "$aw :("; 

     redo PROMPT; 
    } 
} 

say "End"; 
0

如果info可以在一個單獨的進程中運行,那麼你可以只使用fork。否則,你將不得不使用perl的線程版本。

使用fork的例子:

sub start_info { 
    my @params = @_; 
    my $pipe; 
    my $pid = open($pipe, "-|"); 

    if (!$pid) { 
    # this code will run in a sub-process 
    # compute $result from @params 
    # and print result to STDOUT 
    sleep(10); 
    my $result = "p = $params[0] - pid $$"; 
    print $result; 
    exit(0); 
    }; 

    my $r; 
    return sub { 
    return $r if defined($r); 
    $r = <$pipe>; # read a single line 
    waitpid $pid, 0; 
    $r; 
    }; 
} 

sub prompt { 
    print "Hit return: "; 
    <STDIN>; 
} 

my $info1 = start_info(4); 
prompt(); 
print "result = ", $info1->(), "\n"; 

my $info2 = start_info(30); 
prompt(); 
print "result = ", $info2->(), "\n"; 
+0

這不是我所需要的。 –

+0

然後,爲了獲得任何幫助,您需要更清楚地瞭解您需要什麼。 – ErikR

+0

我編輯了這個例子。對我來說,問題很明顯,所以我不知道我能說什麼。 –

相關問題