zaxxon
October 25, 2012, 8:19am
1
Hi,
I am somewhat new to Perl and currently checking it out. I have a problem testing, if there is nothing being piped in to that script.
I am reading input from STDIN this way:
while( defined($line = <STDIN>) ) {
chomp($line);
if( $line =~ m/($ARGV[0])/g ) {
$count++;
}
}
In other words, my script works fine but when there is nothing being piped in, it just stops and waits for input.
I have tried several things I found on different pages but I wasn't able to get it to work.
Is there a simple way to check and avoid this?
Thanks in forward!
Hi zaxxon,
are you sure that this is the problematic code fragment? I am asking because when I tried this with a command line such as
gzip some_file|perl -e '<your code>' some_arg
, the perl process did not wait (the supposed pipe-writer is not writing anything to the pipe) and I got my prompt back.
EDIT: Are you trying to check if the perl script is reading from a pipe?
In that case, may be you could use the file test operator -t
with STDIN
like:
if (-t STDIN) { die "Input from terminal not allowed\n" }
at the beginning, to ensure that the standard input is not associated with a terminal.
msabhi
October 25, 2012, 8:54am
3
elixir_sinari:
Hi zaxxon,
are you sure that this is the problematic code fragment? I am asking because when I tried this with a command line such as
gzip some_file|perl -e '<your code>' some_arg
, the perl process did not wait (the supposed pipe-writer is not writing anything to the pipe) and I got my prompt back.
Myself too...i got the prompt back when i piped a empty file
cat temp7 | perl -e 'while( defined($line = <STDIN>) ) {
chomp($line);
if( $line =~ m/($ARGV[0])/g ) {
$count++;
}
} print $count;' "END"
But when simply executed like below without piping anything
perl -e 'while( defined($line = <STDIN>) ) {
chomp($line);
if( $line =~ m/($ARGV[0])/g ) {
$count++;
}
} print $count;' "END"
It waits and i guess its just waiting for input..i believe this is per design..
zaxxon
October 25, 2012, 8:56am
4
Hey,
my problem is to catch the issue, that there is nothing being piped in. Example:
$ cat mach.pl
#!/usr/bin/perl
while( defined($line = <STDIN>) ) {
chomp($line);
print $line . "\n";
}
exit(0);
Yes, feeding it works:
$ echo yo | ./mach.pl
yo
This hangs and waits for input, like a grep
in a shell waiting for input. Can only break it with Ctrl+c:
$ ./mach.pl
...
I am looking for a way to notice, that there is nothing to be read from STDIN so I can end the script with some error message.
Check my earlier post now.
$ cat mach.pl
#!/usr/bin/perl
if (-t STDIN) { die "Input from terminal not allowed\n" }
while( defined($line = <STDIN>) ) {
chomp($line);
print $line . "\n";
}
exit(0);
3 Likes
zaxxon
October 25, 2012, 9:14am
6
Works perfect, thanks a lot
Edit:
If not too lengthy/complicated other solutions are welcome. Maybe I grasp a tad more of PERL
Scott
October 25, 2012, 9:23am
7
I've used this in the past, but not sure if it's a good way. Like you, I'm quite new to the language
$ cat MyScript
if(tell(\*STDIN) == -1) {
printf( "got file\n\n");
}
$ ./MyScript
$
$ cat X | ./MyScript
got file
$
Hey, I am learning this magical beast, too.