Created
January 24, 2013 12:00
-
-
Save cutthroat/4620655 to your computer and use it in GitHub Desktop.
A (possibly incorrect) way to get non-blank input from STDIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub non_blank_or_prompt { | |
my $input = shift; | |
if (length($input // '') < 1) { | |
# Check for tty. However, consider IO::Interactive | |
-t STDIN and -t STDERR | |
or die "Input required"; | |
for (;;) { | |
print STDERR "Enter something: "; | |
defined ($input = <STDIN>) | |
or die "\nInput required"; | |
chomp $input; | |
last if length($input) > 0; | |
print STDERR "Try again\n"; | |
} | |
} | |
$input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment