Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Need pipe and parameter help

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


in reply to Re: Need pipe and parameter help
in thread Need pipe and parameter help

Thank you!


$ ( 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


-T

Replies are listed 'Best First'.
Re^3: Need pipe and parameter help
by Todd Chester (Scribe) on Nov 17, 2016 at 08:36 UTC

    ./ReadAPipe.pl a b c

    hangs with an empty pipe
    how do I fix that?

      hangs with an empty pipe how do I fix that?

      By only reading from it if it is a pipe...

      #!/usr/bin/perl # http://perlmonks.org/?node_id=1176039 use strict; use warnings; my $Pipe = ''; if( -p STDIN ) { while( <STDIN> ) { $Pipe .= $_; } } 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__

      Will prevent that hang:

      C:\test>ReadPipeAndArgs.pl A B CDE H 123 This was read from the pipe: <> This was the read from the parameters: <A B CDE H 123>

      But it prevents supplying the data by stdin redirection rather than a pipe:

      C:\test>ReadPipeAndArgs.pl A B CDE H 123 < ReadPipeAndArgs.pl This was read from the pipe: <> This was the read from the parameters: <A B CDE H 123>

      Which would work without the pipe test.

      So, perhaps a better way would be to only read from STDIN, if it is not connected to a terminal, which allows both modes of input and prevents the hang if neither is supplied:

      C:\test>ReadPipeAndArgs.pl A B CDE H 123 < ReadPipeAndArgs.pl This was read from the pipe: <#!/usr/bin/perl # http://perlmonks.org/?node_id=1176039 use strict; use warnings; my $Pipe = ''; unless( -t STDIN ) { while( <STDIN> ) { $Pipe .= $_; } } 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__ > This was the read from the parameters: <A B CDE H 123> C:\test>type ReadPipeAndArgs.pl | ReadPipeAndArgs.pl A B CDE H 123 This was read from the pipe: <#!/usr/bin/perl # http://perlmonks.org/?node_id=1176039 use strict; use warnings; my $Pipe = ''; unless( -t STDIN ) { while( <STDIN> ) { $Pipe .= $_; } } 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__ > This was the read from the parameters: <A B CDE H 123> C:\test>ReadPipeAndArgs.pl A B CDE H 123 This was read from the pipe: <> This was the read from the parameters: <A B CDE H 123>

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

      That's not an empty pipe, just a pipe that has not yet produced a value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-25 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found