Joel G Mathew
Joel G Mathew

Reputation: 8081

User input is terminated only on Ctrl D, instead of newline

While reading user input from my script running on Debian, I found that user input would be terminated only after pressing Ctrl-D, instead of after hitting Enter/Return key.

my $userchoice = <>;

For my script, I require the user to enter text and terminate it with the Return/Enter key. What could be causing this in my script?

Could setting slurp mode earlier in my script be related to this?

I had a look at perdoc, but couldnt find an explanation there.

sub InteractiveMenu {
    for my $key(0 .. $#desclist) {
        my $value = $desclist[$key];
        printf (" %-3s %-20s -> %-15s -> %-30s\n", $key, $desclist[$key], $iplist[$key], $filelist[$key] );
    }
    print "\nAvailable choices:\n";
    printf " (R)oot key installation [installs your public key to remote servers]\n";
    printf " (S)etup remote logging [sets up user account on remotes]\n\n";
    print "Choose a server to work on:\n"; 
    chomp(my $userchoice = <>);
}
sub ListRemotes {
    print "Listing remote servers from $Confile\n";
    open my $ReadHandle, "<", $Confile or die $!;
    local $/; # enable localized slurp mode
    chomp(my $content = <$ReadHandle>);
    close $ReadHandle;
    my @values = split('zone ', $content);
      foreach my $val (@values) {       
        #print $val."\n-------------------------\n";
        &ListWorker($val);
      }
    InteractiveMenu();  
}

enter image description here

Upvotes: 0

Views: 908

Answers (2)

dwindura
dwindura

Reputation: 1

To reset the slurp mode, you can put the slurp mode into a scope

open my $ReadHandle, "<", $Confile or die $!;
{
    local $/; # enable localized slurp mode
    chomp(my $content = <$ReadHandle>);
}
close $ReadHandle;

Upvotes: 0

ikegami
ikegami

Reputation: 386396

Could setting slurp mode earlier in my script be related to this?

Yes. Setting $/ to undef causes readline (aka <>) to read until the end of the file instead of until the end of the line. Ctrl-D causes your terminal to signal EOF.

Upvotes: 2

Related Questions