working with other programs in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting working with other programs in perl
# 1  
Old 01-08-2009
working with other programs in perl

Hi,

I'm still new to perl, and I'm trying to figure out how to work with data output from another program. For example, from a command line I can run "foo -xyz" and it will produce the output I am looking for, which is several lines of text that I will then parse and manipulate within the perl script.

I could redirect the output to a file and then read that file to do what I need to do, but isn't there a way to use the data directly.. line by line.. as it comes out? as STDIN perhaps?

-Decoy
# 2  
Old 01-08-2009
Code:
foo -xyz | perl '...'

# 3  
Old 01-08-2009
yes, but I'm trying to do it internal of the script.

I'm thinking:
Code:
$run=`foo -xyz`
@lines=split(/\n/,$run)

but I'm concerned with running into resource issues as sometimes the output of foo can be very large. I was hoping there was another approach.
# 4  
Old 01-08-2009
On your command line pipe the stdout of the command to the stdin in of your perl script:

Code:
foo | yourscript

then in yourscript do this:


Code:
# This loop reads from STDIN
    while (<>) {
       #do what you need to do with $_;
}

Example:

Code:
$ cat yourscript.pl
#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
        print $_;
}

exit 0;

$ cal | yourscript.pl
    January 2009
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Try these websites:

perlintro - perldoc.perl.org

Learn Perl - Learn Perl

perlfaq
# 5  
Old 01-08-2009
Do read line by line when running from inside a script use open
Code:
open $FOO, 'foo -xyz|';
while(<FOO>) {
    print $_;
}
close $FOO;

From perldoc -f open:
Quote:
If the filename begins with '|' , the filename is interpreted as a command to which output is to be piped, and if the filename ends with a '|' , the filename is interpreted as a command which pipes output to us.
# 6  
Old 01-08-2009
Thanks guys
# 7  
Old 01-09-2009
Code:
#! /usr/bin/perl
open FH,"cat a.txt|";
while(<FH>){
print $_;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Net::IP not working

Experts - I have a snippet of code I can't figure out. I was hoping someone could help me here. I have a file of IPv6 address that I need to format correct. Example in: 2620:0:2d0:200::7 2620:0:2d0:200:a0:c 2620:0a:3f0:200:a0:c I need to convert them to:... (3 Replies)
Discussion started by: timj123
3 Replies

2. UNIX for Dummies Questions & Answers

Difference between inbuilt suid programs and user defined root suid programs under bash shell?

Hey guys, Suppose i run passwd via bash shell. It is a suid program, which temporarily runs as root(owner) and modifies the user entries. However, when i write a C file and give 4755 permission and root ownership to the 'a.out' file , it doesn't run as root in bash shell. I verified this by... (2 Replies)
Discussion started by: syncmaster
2 Replies

3. Shell Programming and Scripting

Elsif not working in perl

have issue where my elsif is always failing. Basically i have a file with sets of 2 lines, the 1st line that containing "ipwr" and the 2nd line containing a value or "unknown". if the 2nd line contains a value then i want to print the pair of lines. open (INFO, "temp.txt") or die; ... (3 Replies)
Discussion started by: johnny921
3 Replies

4. Shell Programming and Scripting

Remove duplicates, tried several programs not working

Hi all I tried to remove duplicates using all code mentione dbelow but its not wroking it's seems busy please check it sort -u filename.txt perl -lne 'chomp; if(!defined $x{$_}){$x{$_}=1;print}' inputfile out of attached files log file(input) log2file(output) (9 Replies)
Discussion started by: manigrover
9 Replies

5. Shell Programming and Scripting

IF-THEN-ELSE in PERL not working

Guys, i was trying a simple if-then-else statement in perl; but not getting any success in that. can you please help, where i am wrong. I tried $diff variable with double quotes as well, but no go. $region = $ARGV; $diff = $ARGV; if ; then ($date) = split(' ', `ssh -xC $san cat... (2 Replies)
Discussion started by: sdosanjh
2 Replies

6. Shell Programming and Scripting

perl if else if loop not working

I am trying to work with a text file which has following format and trying ti run if elseif loop but fails. Any help is appreciated 289B ship N-grp 123 289C ship N-grp 123 289D ship N-grp 123 2CE1 flight N-grp 123 2CE2 flight N-grp 123 2CE3 flight N-grp 123 2CE4 flight N-grp 123 2DAF... (4 Replies)
Discussion started by: dynamax
4 Replies

7. Shell Programming and Scripting

\K in perl not working

Hi All, I have just started learning perl and was working on my one-liners tips and tricks. Instead of using the below command : perl -lape 's/(^From:).*/$1 Nelson Elhage <nelhage\@ksplice.com>/' i tried using the \k command using the below command but it gave no results: perl -lape... (2 Replies)
Discussion started by: kunwar
2 Replies

8. Shell Programming and Scripting

Running programs in perl

I am trying to run a program called GMT using perl. Cannot make to run. I have tried using exec("date"); as a test but when I use exec($try); nothing happens. #!/usr/bin/perl print "$#ARGV\n"; if ($#ARGV != 3) { print "usage: jcdplot.perl\n"; exit; } $h = $ARGV;... (1 Reply)
Discussion started by: kristinu
1 Replies

9. UNIX for Dummies Questions & Answers

Are programs like sys_open( ) ,sys_read( ) et al examples of system level programs ?

Are the programs written on schedulers ,thread library , process management, memory management, et al called systems programs ? How are they different from the programs that implement functions like open() , printf() , scanf() , read() .. they have a prefix sys_open, sys_close, sys_read etc , right... (1 Reply)
Discussion started by: vishwamitra
1 Replies

10. Shell Programming and Scripting

Perl call C programs

Hi, I am a beginner in Perl programming. Now i need to call a C program from a perl program ...Can any one please help me and give any details how i can do this. Thanks and Regards (3 Replies)
Discussion started by: gjithin
3 Replies
Login or Register to Ask a Question