Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Empty STDIN does not exit while loop

by haukex (Bishop)
on May 20, 2017 at 08:28 UTC ( #1190704=note: print w/replies, xml ) Need Help??


in reply to Empty STDIN does not exit while loop

In general I'd recommend avoiding the pattern while(chomp(<$handle>)) because it often suffers from issues (another explanation).

You might also want to consider using a module for prompting.

use IO::Prompt 'prompt'; my $resp = prompt("Foo? ", -yn); use Term::ReadLine; my $term = Term::ReadLine->new; my $line = $term->readline("Bar? ");

Or even this (the advantage being ExtUtils::MakeMaker is a core module, even though it has a completely different purpose):

use ExtUtils::MakeMaker 'prompt'; my $value = prompt("Quz?","n");

The module does have another advantage: "If prompt() detects that it is not running interactively and there is nothing on STDIN or if the PERL_MM_USE_DEFAULT environment variable is set to true, the $default will be used without prompting. This prevents automated processes from blocking on user input."

Update 2020-11-30: See also the more powerful IO::Prompter, simple example here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190704]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-07-29 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?