![]() |
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 |
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 |
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 |
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 |"); |
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 |
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 |
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 |
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 |
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. |
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.