Read stdout into perl

S

Sean Berry

I have the following...

open (DATA, "|/usr/bin/gunzip -c $filename");
while(<DATA>) {
...
}

But this does not work.

How do I read stdout into PERL?

Thanks
 
A

A. Sinan Unur

I have the following...

open (DATA, "|/usr/bin/gunzip -c $filename");

1. DATA is a special filehandle in Perl. You might want to leave it
alone.

2. You should always, yes *always* check the return value of open.

3. You need to read the posting guidelines for this group. The
likelihood of getting a useful response increases exponentially if you
follow the few simple suggestions outlined there.

while(<DATA>) {
...
}

But this does not work.

You should also consult

How do I read stdout into PERL?

You are expected to consult the FAQ before posting.

Sinan
 
S

Sherm Pendley

Sean Berry said:
I have the following...

open (DATA, "|/usr/bin/gunzip -c $filename");

Always, yes *always* check the return value of open():

open(DATA, "/usr/bin/gunzip -c $filename |") or
die "Could not open /usr/bin/gunzip -c $filename: $!";
But this does not work.

How do I read stdout into PERL?

By opening the child process for reading instead of writing - pay
attention to where the "|" goes.

Have a look at "perldoc perlopentut" for a good tutorial on using
open().

sherm--

PS: The language is Perl, not PERL.
 
S

Sean Berry

I have the following...
open (DATA, "|/usr/bin/gunzip -c $filename");
while(<DATA>) {
...
}

But this does not work.

How do I read stdout into PERL?

Sorry, looked for the answer for 20 minutes,
posted this, then found the answer 1 minute later.

open(DATA, "/usr/bin/gunzip -c $filename |");
 
A

A. Sinan Unur

Sorry, looked for the answer for 20 minutes,
posted this, then found the answer 1 minute later.

open(DATA, "/usr/bin/gunzip -c $filename |");

What can I say?

*PLONK*
 
J

John W. Krahn

Sean said:
I have the following...

open (DATA, "|/usr/bin/gunzip -c $filename");
while(<DATA>) {
...
}

But this does not work.

How do I read stdout into PERL?

The same way you would do it on the command line:

open DATA, "/usr/bin/gunzip -c $filename |";



John
 
A

A. Sinan Unur

open DATA, "/usr/bin/gunzip -c $filename |";

Are you sure it is a good idea to re-enforce the OP's bad habits.

Now, granted, most likely, nothing to bad will happen by using DATA to
mean something other than what everyone else expects it to mean. And, most
likely, the $filename exists, gunzip is where the OP expects it so on and
so forth, but why not do:

open my $data, '-|', "/usr/bin/gunzip -c $filename"
or die "Failed to open pipe /usr/bin/gunzip -c $filename: $!";

while(<$data>) {
# ...
}

Sinan
 
J

John W. Krahn

A. Sinan Unur said:
Are you sure it is a good idea to re-enforce the OP's bad habits.

Now, granted, most likely, nothing to bad will happen by using DATA to
mean something other than what everyone else expects it to mean. And, most
likely, the $filename exists, gunzip is where the OP expects it so on and
so forth, but why not do:

Thanks. :) In the same spirit, may I correct your spelling mistake? Too
should be spelt with two o's as in "nothing too bad".


John
 
B

Bart Lateur

Sean said:
I have the following...

open (DATA, "|/usr/bin/gunzip -c $filename");
while(<DATA>) {
...
}

But this does not work.

How do I read stdout into PERL?

By putting the "|" at the end of the line.

open (DATA, "/usr/bin/gunzip -c $filename |");

If you want bidirectional piping, thus, both piping to and from your
app, check out IPC::Open2 (and IPC::Open3), both standard modules.

<http://perldoc.perl.org/IPC/Open2.html>
<http://perldoc.perl.org/IPC/Open3.html>
 
T

T Beck

Are you sure it is a good idea to re-enforce the OP's bad habits.


I find it interesting that nobody has mentioned that it's rare-to-never
that a piped command dies on open. He should be checking for errors on
CLOSE
my $pipename = "/usr/bin/gunzip -c $filename"
open PIPE, $pipename |" or die "Failed to open pipe $pipename\n";
while(<PIPE>)
{}

close PIPE or die "Broken pipe $pipename\n";
 

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
474,265
Messages
2,571,071
Members
48,771
Latest member
ElysaD

Latest Threads

Top