Storing results in array rather than printing

J

joseph85750

I have a unix program called zathras which will generate some useful
output results when a file is piped into it.

ie:

cat /path/to/asci-file.txt | zathras
(this will output some text I wish to capture)

I'm trying to write a perl script to execute the above, but not from a
file piped in. Instead, a variable within the perl script, since the
asci-file.txt above will have changing content. Here's what I have so
far:

$someData="12 15 77 19 42 2112 99 88"; #data from asci-file.txt

open (FOO,"|zathras");

print FOO $someData;
close;

This works, and behaves exactly as running the program directly from
unix. But what I really want is to store the results in an array,
rather than printing.

I suspected I could do something like:

@array=<FOO $someData>;

But this does not work. The @array ends up empty, but no errors are
thrown.

Any help would be great.

Thanks!
 
P

Paul Lalli

I have a unix program called zathras which will generate some useful
output results when a file is piped into it.

ie:

cat /path/to/asci-file.txt | zathras
(this will output some text I wish to capture)

I'm trying to write a perl script to execute the above, but not from a
file piped in. Instead, a variable within the perl script, since the
asci-file.txt above will have changing content. Here's what I have so
far:

$someData="12 15 77 19 42 2112 99 88"; #data from asci-file.txt

open (FOO,"|zathras");

print FOO $someData;
close;

This works, and behaves exactly as running the program directly from
unix. But what I really want is to store the results in an array,
rather than printing.

Sounds like you need to open this zathras program for both reading and
writing. For that, you want the IPC::Open2 module:

[untested]
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open2;

my $someData="12 15 77 19 42 2112 99 88";
my $pid = open2(my ($read_fh, $write_fh), 'zathras')
or die "Cannot open zathras: $!";
print $write_fh $someData;
my @results = <$read_fh>;
#do whatever you want with @results;
__END__

For more info, see `perldoc IPC::Open2`

Hope this helps,
Paul Lall
 
X

xhoster

I have a unix program called zathras which will generate some useful
output results when a file is piped into it.

ie:

cat /path/to/asci-file.txt | zathras
(this will output some text I wish to capture)

I'm trying to write a perl script to execute the above, but not from a
file piped in. Instead, a variable within the perl script, since the
asci-file.txt above will have changing content. Here's what I have so
far:

$someData="12 15 77 19 42 2112 99 88"; #data from asci-file.txt

open (FOO,"|zathras");

print FOO $someData;
close;

This works, and behaves exactly as running the program directly from
unix. But what I really want is to store the results in an array,
rather than printing.

I suspected I could do something like:

@array=<FOO $someData>;

my @array = `echo '$someData' | zathras`;

This could be problematic if the shell interprets the characters in
$someData (particular if $someData contains quotes, or on some systems,
newlines)

Also, IPC::Open2 would work, but getting the buffering right is not trivial
if $someData is big and you are not used to that kind of thing.


Xho
 
G

Guest

I have a unix program called zathras which will generate some useful
output results when a file is piped into it.

ie:

cat /path/to/asci-file.txt | zathras
(this will output some text I wish to capture)
[...]

Xhoster's advice is good. In addition, you might consider using Expect.pm.
 
J

joseph85750

my @array = `echo '$someData' | zathras`;

This could be problematic if the shell interprets the characters in
$someData (particular if $someData contains quotes, or on some systems,
newlines)

Also, IPC::Open2 would work, but getting the buffering right is not trivial
if $someData is big and you are not used to that kind of thing.

Xho


Thanks for the replies, everyone. I couldn't get IPC::Open2 to work,
but I understand the need now. The system call solution:

@array = `echo '$someData' | zathras`

worked, and I'll stick with that. I was hoping to avoid a system
call, but I'm tired.

Thanks again!
 
U

Uri Guttman

j> Thanks for the replies, everyone. I couldn't get IPC::Open2 to work,
j> but I understand the need now. The system call solution:

j> @array = `echo '$someData' | zathras`

first off, that is not a system() call but backticks. they are related
but not the same thing.

j> worked, and I'll stick with that. I was hoping to avoid a system
j> call, but I'm tired.

open2 does the same fork/exec as do system and backticks. why do you
think you are avoiding a system call? you have to fork/exec zathras
somehow and perl has about 5 different (all useful) ways to do that. the
only think you avoid with open2 vs backticks is the shell call to parse the
command there.

uri
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top