Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Read stdout into perl (http://www.velocityreviews.com/forums/t893286-read-stdout-into-perl.html)

Sean Berry 07-14-2005 12:34 AM

Read stdout into perl
 
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. Sinan Unur 07-14-2005 12:42 AM

Re: Read stdout into perl
 
"Sean Berry" <sean@buildingonline.com> 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 <1usa@llenroc.ude.invalid>
(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

Sherm Pendley 07-14-2005 12:43 AM

Re: Read stdout into perl
 
"Sean Berry" <sean@buildingonline.com> 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

Sean Berry 07-14-2005 12:45 AM

Re: Read stdout into perl
 
>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. Sinan Unur 07-14-2005 12:51 AM

Re: Read stdout into perl
 
"Sean Berry" <sean@buildingonline.com> 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 <1usa@llenroc.ude.invalid>
(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

John W. Krahn 07-14-2005 12:55 AM

Re: Read stdout into perl
 
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

A. Sinan Unur 07-14-2005 01:03 AM

Re: Read stdout into perl
 
"John W. Krahn" <someone@example.com> 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 <1usa@llenroc.ude.invalid>
(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

John W. Krahn 07-14-2005 01:17 AM

Re: Read stdout into perl
 
A. Sinan Unur wrote:
> "John W. Krahn" <someone@example.com> 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

Bart Lateur 07-14-2005 01:21 PM

Re: Read stdout into perl
 
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.

T Beck 07-14-2005 05:01 PM

Re: Read stdout into perl
 
"A. Sinan Unur" <1...@llenroc.ude.invalid> 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";



All times are GMT. The time now is 07:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57