note
golux
Hi burszuras,
<p>You may also be interested in the [doc://-X|-t file test] (on line 23):
<c>
-t Filehandle is opened to a tty.
</c>
which is a nice way to discover whether you're at the tail end of a pipe or not.
</p><p>For example:
<c>
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
use File::Basename;
my $iam = basename($0);
if (-t *STDIN) {
my $nargs = @ARGV;
say "Script '$iam' called from a terminal with $nargs arg(s):";
for (my $idx = 0; $idx < $nargs; $idx++) {
printf " %2d. %s\n", $idx+1, $ARGV[$idx];
}
} else {
say "Script '$iam' is receiving input from a pipe";
}
</c>
Call the above script like this: <c>./script arg1 arg2 arg3</c> and you get:
<c>
Script 'script' called from a terminal with 3 arg(s):
1. arg1
2. arg2
3. arg3
</c>
If you call it like this, instead: <c>echo Pipe to script | ./script</c>
then you'll get this:
<c>
Script 'script' is receiving input from a pipe
</c>
In the latter case, you could then do:
<c>
while (<STDIN>) {
say "Got input: $_";
}
</c>
or similar, to handle the piped data as you desire.
<div class="pmsig"><div class="pmsig-941867">
<center><font size="-1">
<font color="#ff0000">say </font>
<font color="#ffbf3f">substr</font><font color="#c8871a">+</font><font color="#9f4f06">lc </font><font color="#711f79">crypt</font><font color="black">(<font color="#a9df2d">qw </font><font color="#4df9ff">$i3 </font><font color="#4db2ff">SI$</font>),4,5</font></font></center>
</div></div>
1196720
1196720