简体   繁体   English

在后台运行子例程,直到出现条件

[英]Run a subroutine in the background until a condition

How could I rewrite this so that info runs in the background until a $aw is equal result ? 我该如何重写它,以便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";

I agree with user5402. 我同意user5402。 The mention of running in the background and the sleeping info function raise a lot of questions. 提到在后台运行和睡眠信息功能引起了很多问题。

I wonder if maybe you're looking for a more tidy way of re-prompting for input when the given input is not correct. 我想知道您是否正在寻找一种在给定输入不正确时重新提示输入的更整洁的方法。 If that's the case, then maybe the IO::Prompter module works for you. 如果真是这样,那么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";

If info can run in a separate process then you can just use fork . 如果info可以在单独的进程中运行,那么您可以使用fork Otherwise you will have to use a threaded version of perl. 否则,您将不得不使用perl的线程版本。

Example of using fork : 使用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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM