How to get Perl to use different pens to enter pipes and keyboard input?

I have a Perl script processing pipe. At some point, I would like the script to stop and ask for a custom keyboard. my $input = <STDIN>; does not work. It just reads the next line from the pipe. How can I get Perl to use different handles for channel input and keyboard input?

+6
input perl pipe
source share
1 answer

If you are on a Unix platform, you can open the file descriptor /dev/tty (or use IO::Pty ).
A good example of working with tty is given in the example "Testing the program Interactively": http://pleac.sourceforge.net/pleac_perl/userinterfaces.html

You should also consider entering a password through Term::ReadKey (described in perlfaq8 ) - I think this can be tied to TTY instead of STDIO, but I'm not sure. If this is not the case, use the TTY + Term :: ReadKey solution indicated at the end of this SO response from brian d foy .

Here is an example.

This is not the best style (does not use the 3-arg form open , and also uses lexical file descriptors) , but it should work.

 use autodie; # Yay! No "or die '' " use Term::ReadKey; open(TTYOUT, ">/dev/tty"); print TTYOUT "Password?: "; close(TTYOUT); open(TTY, "</dev/tty"); ReadMode('noecho'); $password = ReadLine(0, *TTY); 
+12
source share

All Articles