2010-03-06 92 views
2

在我的短信我的腳本在文本閱讀與該子程序發送:什麼是閱讀文本的好方法?

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

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

這樣,我只能通過取消所有,直到我到了錯誤糾正錯誤,只有這最後一行。 有沒有更好的方法來閱讀我的文字?

+0

-1你只能讀取輸入的一行。沒有嘗試檢測和/或更正您發佈的代碼中的任何錯誤。 – 2010-03-06 14:29:26

+0

我以爲會有一些模塊。我現在寫了一個validate_input-routine,但也許我更喜歡用$ tempfile中的「system('vim',$ tempfile)/ read」。 – 2010-03-06 17:51:47

回答

2

這是 「正常」 的方式來讀取輸入:

use strict; 
use warnings; 

# ... 

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

    # do something else with $input...  

} 
1

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

my $is_valid = 0; 
my $input; 

while (!$is_valid) { 
    print "Enter something: "; 
    $input = <>; 
    $is_valid = validate_input($input); 
}  
相關問題