2

在我的 sms 脚本中,我阅读了要使用此子例程发送的文本:

my $input = input( "Enter the input: " );

sub input {
    my $arg = shift;
    print $arg if $arg;
    print "\n ";
    chomp( my $in = <> );
    return $in;
}

这样,我只能通过取消所有来纠正错误,直到我遇到错误,并且这只在最后一行。有没有更好的方法来阅读我的文字?

4

2 回答 2

2

这是读取输入的“正常”方式:

use strict;
use warnings;

# ...

while (my $input = <>)
{
    chomp $input;
    handle_error($input), next unless validate($input);

    # do something else with $input...    

}
于 2010-03-06T16:35:07.980 回答
1

您可以在input子例程中使用 while 循环,例如

my $is_valid = 0;
my $input; 

while (!$is_valid) {   
    print "Enter something: ";  
    $input = <>;  
    $is_valid = validate_input($input);
}      
于 2010-03-06T14:54:00.417 回答