package ReadPipe; require Exporter; use strict; use warnings; use diagnostics; # use diagnostics "-verbose"; use Term::ReadKey qw ( ReadKey ReadMode ); # our @ISA = qw( Exporter ); use Exporter qw(import); our @EXPORT = qw(); # symbols to be exported by default (space-separated) our $VERSION = 0.01; our @EXPORT_OK = qw( ReadPipe ); # Refernece: https://metacpan.org/pod/Term::ReadKey # ReadMode MODE [, Filehandle] # # Takes an integer argument, which can currently be one of the following values: # # 0 Restore original settings. # 1 Change to cooked mode. # 2 Change to cooked mode with echo off. # (Good for passwords) # 3 Change to cbreak mode. # 4 Change to raw mode. # 5 Change to ultra-raw mode. # (LF to CR/LF translation turned off) # # Or, you may use the synonyms: # # restore # normal # noecho # cbreak # raw # ultra-raw # # # ReadKey MODE [, Filehandle] # # Takes an integer argument, which can currently be one of the following values: # # 0 Perform a normal read using getc # -1 Perform a non-blocked read # >0 Perform a timed read sub ReadPipe () { # if data is not present in the pipe (STDIN), return an empty sting # if data is present, read until an EOF is detected, then return the # string, less the EOF mark my $Pipe = ""; my $Key = ""; ReadMode 4; # Turn off controls keys if ( defined ( $Key = ReadKey ( -1, \*STDIN ) ) ) { $Pipe = $Key; while ( ) { $Pipe .= $_ }; # reads until EOF detected } ReadMode 1; # Reset tty mode before exiting chomp ( $Pipe ); # print STDERR "\$Pipe = <$Pipe>\n"; return $Pipe; }