Read stdout into perl

Discussion in 'Perl Misc' started by Sean Berry, Jul 14, 2005.

  1. Sean Berry

    Sean Berry Guest

    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
    Sean Berry, Jul 14, 2005
    #1
    1. Advertising

  2. "Sean Berry" <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #2
    1. Advertising

  3. "Sean Berry" <> 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
    Sherm Pendley, Jul 14, 2005
    #3
  4. Sean Berry

    Sean Berry Guest

    >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 |");
    Sean Berry, Jul 14, 2005
    #4
  5. "Sean Berry" <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #5
  6. 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
    John W. Krahn, Jul 14, 2005
    #6
  7. "John W. Krahn" <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #7
  8. A. Sinan Unur wrote:
    > "John W. Krahn" <> 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
    John W. Krahn, Jul 14, 2005
    #8
  9. Sean Berry

    Bart Lateur Guest

    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.
    Bart Lateur, Jul 14, 2005
    #9
  10. Sean Berry

    T Beck Guest

    "A. Sinan Unur" <> 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";
    T Beck, Jul 14, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfred von Campe

    How to read STDOUT from a Perl function

    Alfred von Campe, Jul 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    14,400
    cr4ft
    Jan 5, 2009
  2. =?ISO-8859-2?Q?Jacek_Pop=B3awski?=

    read stdout/stderr without blocking

    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=, Sep 12, 2005, in forum: Python
    Replies:
    1
    Views:
    3,985
    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=
    Sep 13, 2005
  3. Elad
    Replies:
    0
    Views:
    401
  4. Andreas S
    Replies:
    3
    Views:
    243
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page