Perl: How to check if there is something being piped in


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: How to check if there is something being piped in
# 1  
Old 10-25-2012
Perl: How to check if there is something being piped in

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:
Code:
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!

Last edited by zaxxon; 10-25-2012 at 09:25 AM.. Reason: spelling
# 2  
Old 10-25-2012
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
Code:
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:
Code:
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.

Last edited by elixir_sinari; 10-25-2012 at 09:58 AM..
# 3  
Old 10-25-2012
Quote:
Originally Posted by 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
Code:
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
Code:
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
Code:
 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..
# 4  
Old 10-25-2012
Hey,

my problem is to catch the issue, that there is nothing being piped in. Example:
Code:
$ cat mach.pl
#!/usr/bin/perl

while( defined($line = <STDIN>) ) {
        chomp($line);
        print $line . "\n";
}

exit(0);

Yes, feeding it works:
Code:
$ 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:
Code:
$ ./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.
# 5  
Old 10-25-2012
Check my earlier post now.
Code:
$ 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);

These 3 Users Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-25-2012
Works perfect, thanks a lot Smilie

Edit:
If not too lengthy/complicated other solutions are welcome. Maybe I grasp a tad more of PERL Smilie
# 7  
Old 10-25-2012
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 Smilie
Code:
$ cat MyScript
if(tell(\*STDIN) == -1) {
  printf( "got file\n\n");
}

$ ./MyScript
$
$ cat X | ./MyScript
got file

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Can't Output Piped Perl In-line command to a File

Hello, I'm pretty stumped, and I don't know why I am not able to redirect the output to the 'graphme' file with the command below in Fedora 18. tcpdump -l -n -t "tcp == 18" | perl -ane '($s,$j)=split(/,/,$F,2); print "$s\n";' > graphme In case you're wondering, I was following the example... (2 Replies)
Discussion started by: ConcealedKnight
2 Replies

3. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

4. Shell Programming and Scripting

Trouble executing piped shell commands in perl code

I am trying to execute a piped combination of shell commands inside a perl program. However, it is not working as desired. This is my program, i am trying to print only filenames from the output of ls -l $ cat list_test #!/usr/bin/perl -w use strict; my $count=0; my @list=`ls -l|awk... (4 Replies)
Discussion started by: sam05121988
4 Replies

5. Shell Programming and Scripting

Perl- check the port used

hi everybody; my code is cheking if a port is an actif or not with the cmd netstat -ln,I want first to enter the number of the port which I want to check it but I think that the value of $con in the second "if" is always "0" so the code give me always that the port is not used!!! ... (5 Replies)
Discussion started by: bassma
5 Replies

6. Shell Programming and Scripting

Please check perl script

#!/usr/local/bin/perl #$path = perl; #use File::stat; use Time::localtime; sub ExampleFiles{ $today = time; $today -= $today % 86400; $return_value = 0; $mtime = (stat("$_")) || die "cannot stat file $!"; $size = (stat("$_")); # size in... (3 Replies)
Discussion started by: ellechim
3 Replies

7. Shell Programming and Scripting

number check in perl

Hi,, this is returning true in all cases..( other than 10 dig number also) what could be wrong?? (2 Replies)
Discussion started by: shellwell
2 Replies

8. Shell Programming and Scripting

perl... how to tell if a piped command is still running?

I'm using the fabulous perl. I need a way to tell when a piped call to "open" has completed. Can I do this with a command like <ShellPipe> ?? Reason behind this: I'm trying to write a backup script in perl! This script will download a certain file from my web server, to my computer. Now,... (0 Replies)
Discussion started by: boytheo
0 Replies

9. Shell Programming and Scripting

How to check if perl is installed?

Hi, i'm designing a unix script and i want to know if there is a shell command or a way to see if perl is installed in the system. thanks in advance! (5 Replies)
Discussion started by: kfad
5 Replies

10. Shell Programming and Scripting

Check URL using PERL

I am trying to create a perl script that will make sure a web page can be accessed going through an Apache httpd. The actual content of the web page does not matter. Most likely the web page will just have "You have successfully reached this port." This script will eventually be running... (5 Replies)
Discussion started by: rehoboth
5 Replies
Login or Register to Ask a Question