Skip to content

Instantly share code, notes, and snippets.

@cutthroat
Created January 24, 2013 12:00
Show Gist options
  • Save cutthroat/4620655 to your computer and use it in GitHub Desktop.
Save cutthroat/4620655 to your computer and use it in GitHub Desktop.
A (possibly incorrect) way to get non-blank input from STDIN
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