in reply to Re^3: Need pipe and parameter help
in thread Need pipe and parameter help
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>
|
|---|