Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

STDIN Enter key not quite working

by slugger415 (Monk)
on Apr 05, 2021 at 19:49 UTC ( [id://11130858]=perlquestion: print w/replies, xml ) Need Help??

slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, probably a dumb question, but I am using <STDIN> to collect user information on a Perl script on Mac OSX. For some reason pressing Enter moves the cursor to the next line, and only by pressing control-D can I get it to continue. And control-D by itself doesn't do it either. Naturally I'd like the Enter key to be the only thing needed. Am I doing something wrong?

#!/usr/bin/perl print "How many cookies do you want? Enter number: "; my($cookies) = <STDIN>; chomp($cookies); print "You wanted only $cookies?\n";

Thanks -

Replies are listed 'Best First'.
Re: STDIN Enter key not quite working
by choroba (Cardinal) on Apr 05, 2021 at 19:59 UTC
    my($cookies) = <STDIN>;

    By wrapping $cookies into parentheses, you're forcing list context. <STDIN> in list context reads up to the end of file. Ctrl+D sends the end of line.

    If you just want to read a single line, don't use parentheses.

    my $cookies = <STDIN>;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Aha! thank you!

Re: STDIN Enter key not quite working
by Fletch (Bishop) on Apr 05, 2021 at 21:58 UTC

    If you don't mind the CPAN dependenc(y|ies) you might also look at something like IO::Prompter rather than reading from STDIN with the readline operator.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      I would have a lot of hesitancy about using that module because of this: Several features of this module are known to have problems under Windows.

      Here is a simple formula for a command loop. For user input any leading and trailing spaces are allowed (just like std unix commands). 1) print the prompt, 2)get the user line, 3) reject if it doesn't match the desired regex.

      Note: the parens around the print are required.

      use strict; use warnings; my $n_cookies; while ( (print "How many cookies do you want? Enter number: "), $n_cookies=<STDIN> and $n_cookies !~ /^\s*[1-9]\d*\s*$/) { print "invalid input! - try again!\n"; } $n_cookies +=0; # trick to convert to numeric value # which "deletes" leading/trailing zeroes # or a substitution operation would be fine print "$n_cookies cookies requested!"; __END__ Example Run: Note: allowing leading zero requires more complex regex This is just a "concept example" C:...\PerlProjects\Monks>perl commandloop.pl How many cookies do you want? Enter number: invalid input! - try again! How many cookies do you want? Enter number: 0 invalid input! - try again! How many cookies do you want? Enter number: 023 invalid input! - try again! How many cookies do you want? Enter number: 1 23 invalid input! - try again! How many cookies do you want? Enter number: abc invalid input! - try again! How many cookies do you want? Enter number: cookies 34 invalid input! - try again! How many cookies do you want? Enter number: 5 5 cookies requested!
      I made the above into a sub, but I can't find it right now. Virtually none of my programs prompt the user for input in this way in preference to Getopt or Getopt::Long.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11130858]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found