Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Need pipe and parameter help

by Todd Chester (Scribe)
on Nov 17, 2016 at 13:51 UTC ( [id://1176053]=note: print w/replies, xml ) Need Help??


in reply to Need pipe and parameter help

Thank you all for the help.

Figured it out.

To error is human. To really screw things up required a computer!

Perl 5: how to read rom a pipe and command line parameter at the same time:

PeadPipe.pm

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 foll +owing 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

Thank you all for the help.

Figured it out.

To error is human. To really screw things up required a computer!

Perl 5: how to read rom a pipe and command line parameter at the same time:

PeadPipe.pm

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 foll +owing 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 foll +owing 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 th +e # string, less the EOF mark my $Pipe = ""; my $Key = ""; ReadMode 4; # Turn off controls keys if ( defined ( $Key = ReadKey ( -1, \*STDIN ) ) ) { $Pipe = $Key; while ( <STDIN> ) { $Pipe .= $_ }; # reads until EOF detected } ReadMode 1; # Reset tty mode before exiting chomp ( $Pipe ); # print STDERR "\$Pipe = <$Pipe>\n"; return $Pipe; }

ReadAPipe.pl

#!/usr/bin/perl # Note: check out Pause.pm for reading empty keys use lib '/home/linuxutil'; use strict; use warnings; use ReadPipe qw ( ReadPipe ); my $Pipe= ""; # while (<STDIN>){ $Pipe .= $_; } # hangs if pipe is empty $Pipe = ReadPipe::ReadPipe (); print "This was read from the pipe:\n"; print "<$Pipe>\n\n"; print "This was the read from the parameters:\n"; print "<@{ARGV}>\n"; __END__
$ ( echo Hi; sleep 3; echo Bye ) | ./ReadAPipe.pl a b c This was read from the pipe: <Hi Bye> This was the read from the parameters: <a b c> ./ReadAPipe.pl a b c This was read from the pipe: <> This was the read from the parameters: <a b c>
# # # ReadKey MODE [, Filehandle] # # Takes an integer argument, which can currently be one of the foll +owing 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 th +e # string, less the EOF mark my $Pipe = ""; my $Key = ""; ReadMode 4; # Turn off controls keys if ( defined ( $Key = ReadKey ( -1, \*STDIN ) ) ) { $Pipe = $Key; while ( <STDIN> ) { $Pipe .= $_ }; # reads until EOF detected } ReadMode 1; # Reset tty mode before exiting chomp ( $Pipe ); # print STDERR "\$Pipe = <$Pipe>\n"; return $Pipe; }

ReadAPipe.pl

#!/usr/bin/perl # Note: check out Pause.pm for reading empty keys use lib '/home/linuxutil'; use strict; use warnings; use ReadPipe qw ( ReadPipe ); my $Pipe= ""; # while (<STDIN>){ $Pipe .= $_; } # hangs if pipe is empty $Pipe = ReadPipe::ReadPipe (); print "This was read from the pipe:\n"; print "<$Pipe>\n\n"; print "This was the read from the parameters:\n"; print "<@{ARGV}>\n"; __END__
$ ( echo Hi; sleep 3; echo Bye ) | ./ReadAPipe.pl a b c This was read from the pipe: <Hi Bye> This was the read from the parameters: <a b c> ./ReadAPipe.pl a b c This was read from the pipe: <> This was the read from the parameters: <a b c>

Replies are listed 'Best First'.
Re^2: Need pipe and parameter help
by shmem (Chancellor) on Nov 18, 2016 at 11:53 UTC

    Please tidy up that response, it is badly formatted.

    $ ( echo Hi; sleep 3; echo Bye ) | ./ReadAPipe.pl a b c
    This was read from the pipe:
    <Hi
    Bye>
    
    This was the read from the parameters:
    
    
    
    ./ReadAPipe.pl a b c
    This was read from the pipe:
    <>
    
    This was the read from the parameters:
    
    

    Saving your (cleaned up) ReadPipe.pm to a sensible location and running your ReadAPipe.pl, I get:

    qwurx [shmem] ~> ( echo Hi; sleep 3; echo Bye ) | ./ReadAPipe.pl a b c This was read from the pipe: <Hi Bye> This was the read from the parameters: <a b c> qwurx [shmem] ~> ./ReadAPipe.pl a b c This was read from the pipe: <> This was the read from the parameters: <a b c>

    I suspect that the code / result you posted isn't consistent (version mismatch?)

    Please note that the value returned requireing your ReadPipe.pm (which is invoked by use) is the number of elements contained in @EXPORT_OK - which might, or might not, be intended. You should be aware of this side-effect. If your module read

    ... our @EXPORT_OK = qw( ReadPipe ); my $foo; ...

    then loading the module would fail, since the value computed from my $foo is undefined.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      > Please tidy up that response, it is badly formatted.

      Thank you for the tidy up. The formatting used on this form is *obnoxious*. Are there any plans to move to a more user friendly format?

      My last post I copied and pasted from the source section of Komposer. What a pain in the neck!

      My original solution with the messy formatting would not respond properly to
      (sleep 3; echo bye) | ReadAPipe.pl

      The last post cleaned that up

      Many thanks,
      -T

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found