Created
September 29, 2014 04:41
-
-
Save sbuzonas/1bb2281cdff4d989d6da to your computer and use it in GitHub Desktop.
Perl TTY input sample
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
my $name = &promptUser("What is your name", "Steve"); | |
print 'Hello, ', $name, "!\n"; | |
sub promptUser { | |
my ($promptString,$defaultValue) = @_; | |
print $defaultValue | |
? "$promptString? [$defaultValue]: " | |
: "$promptString? "; | |
local *TTY; | |
open(TTY, "<", "/dev/tty"); | |
my $input = <TTY>; | |
chomp($input); | |
if ("$defaultValue") { | |
return $input ? $input : $defaultValue; | |
} else { | |
return $input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment