Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Read stdout into perl

Reply
Thread Tools

Read stdout into perl

 
 
Sean Berry
Guest
Posts: n/a
 
      07-14-2005
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


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-14-2005
"Sean Berry" <(E-Mail Removed)> wrote in news:MqiBe.33011
$ro.13029@fed1read02:

> 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

<URL:http://www.catb.org/~esr/faqs/smart-questions.html>

> How do I read stdout into PERL?


You are expected to consult the FAQ before posting.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      07-14-2005
"Sean Berry" <(E-Mail Removed)> writes:

> 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.

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Sean Berry
Guest
Posts: n/a
 
      07-14-2005
>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 |");



 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-14-2005
"Sean Berry" <(E-Mail Removed)> wrote in news:aBiBe.33012$ro.87
@fed1read02:

>>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 |");


What can I say?

*PLONK*

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-14-2005
Sean Berry wrote:
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-14-2005
"John W. Krahn" <(E-Mail Removed)> wrote in news:5KiBe.123692$9A2.10908
@edtnps89:

> 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

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-14-2005
A. Sinan Unur wrote:
> "John W. Krahn" <(E-Mail Removed)> wrote in news:5KiBe.123692$9A2.10908
> @edtnps89:
>
>>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:


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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      07-14-2005
Sean Berry wrote:

>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>


--
Bart.
 
Reply With Quote
 
T Beck
Guest
Posts: n/a
 
      07-14-2005
"A. Sinan Unur" <(E-Mail Removed)> wrote:
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";

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read STDOUT from a Perl function Alfred von Campe Perl 4 01-05-2009 09:00 AM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
read stdout/stderr without blocking =?ISO-8859-2?Q?Jacek_Pop=B3awski?= Python 1 09-13-2005 07:25 AM



Advertisments