redirecting system output

N

nahum_barnea

Hi.

In my perl script I need to run a software that give output to standard
output.
I need to count the lines of that output.

If I wanted to do it in csh I would do

set a = `sw-tool .... | wc -l`


But I wonna do it with perl and it seems there is no mechanism such as
` ... ` ?

for example

$a = `system "sw-tool | wc -l`;

does not work properly.
is there any clean perl solution except for using temporary file and
reading it?

thankX
NAHUM
 
J

John Bokma

Hi.

In my perl script I need to run a software that give output to standard
output.
I need to count the lines of that output.

If I wanted to do it in csh I would do

set a = `sw-tool .... | wc -l`


But I wonna do it with perl and it seems there is no mechanism such as
` ... ` ?

for example

$a = `system "sw-tool | wc -l`;

does not work properly.
is there any clean perl solution except for using temporary file and
reading it?

Addition: if you want to call sw-tool yourself, you open a pipe for
reading to sw-tool (for example).

What's wrong with the $a line:

- bad variable name
- did you use my $a (I guess not, did you use:

use strict;
use warnings;

I guess not. If I am right, fix this)

- You are calling:

system "sw-tool | wc -l

I doubt if that's what you want to do

perldoc -f system
 
J

John W. Krahn

In my perl script I need to run a software that give output to standard
output.
I need to count the lines of that output.

If I wanted to do it in csh I would do

set a = `sw-tool .... | wc -l`


But I wonna do it with perl and it seems there is no mechanism such as
` ... ` ?

for example

$a = `system "sw-tool | wc -l`;

does not work properly.
is there any clean perl solution except for using temporary file and
reading it?

my $count_of_lines = () = `sw-tool ...`;

Assuming that you haven't changed the value of $/.


John
 
X

Xicheng Jia

Hi.

In my perl script I need to run a software that give output to standard
output.
I need to count the lines of that output.

If I wanted to do it in csh I would do

set a = `sw-tool .... | wc -l`


But I wonna do it with perl and it seems there is no mechanism such as
` ... ` ?

chomp( my $num_lines = `sw-tool .... | wc -l` );

Xicheng
 
T

Tad McClellan

If I wanted to do it in csh I would do

set a = `sw-tool .... | wc -l`


and if you wanted to do it in Perl you would do

$a = `sw-tool .... | wc -l`;

$a = `system "sw-tool | wc -l`;
^^^^^^ ^
^ eh?

system() is a _Perl_ function.

What goes in the backticks are _shell_ commands.

does not work properly.


GIGO.
 
N

nahum_barnea

Thanks to all answers, and specially you Xicheng Jia,
The "chomp" thing realy made my day :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top