views:

79

answers:

2

Hello! In my sms-script I read in the text to send with this subroutine:

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

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

This way I can correct errors only through canceling all until I reach the error and this only in the last line. Is there a better way to read in my text?

+1  A: 

You can use a while loop inside your input subroutine, e.g.

my $is_valid = 0;
my $input; 

while (!$is_valid) {   
    print "Enter something: ";  
    $input = <>;  
    $is_valid = validate_input($input);
}      
eugene y
+2  A: 

This is the "normal" way to read input:

use strict;
use warnings;

# ...

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

    # do something else with $input...    

}
Ether