perl PIPE - need an example of piping in

Discussion in 'Perl Misc' started by seth, Jun 10, 2005.

  1. seth

    seth Guest

    My question is about pips.

    I have the following code:


    EXAMPLE 1:
    #-----------------------------------------------------------
    #!/usr/bin/perl -w

    use strict;

    my $command = "ls -al";
    my @failureMsg;

    open(OUTPUT, "$command |") or die "can't do the command\n";

    my $lines = <OUTPUT>;

    @failureMsg = grep(/\ssp.*\.pl$/, <OUTPUT>);

    print "@failureMsg";
    #-----------------------------------------------------------



    In this code I'm piping my the output of my command to a file
    handle(FH).

    I understand this and am happy with it.

    What I don't understand is the following syntax:


    EXAMPLE 2:
    open(OUTPUT, " | $command") or die "can't do the command\n";


    **note: the example of what I don't understand has no realtion to my
    code snippet above. My example 2 might not be ideal.. but here is what
    I don't undestand.


    Example 1 shows how to "pipe" *into* a FileHandle.

    So is doing something like Example 2 piping *from* a FileHandle?

    Can someone point me to a quick simple example. I've checked most of
    the Orielly series and can't find an example to help me understand what
    the meaning is for prepnding "$command" with the pipe char.

    -thanks
    seth
    seth, Jun 10, 2005
    #1
    1. Advertising

  2. seth

    seth Guest

    okay so I didn't look carefully I did find the following examples in
    the "FileHandle" section of the "programming perl" book

    open(SESAME, "filename") # read from existing file
    open(SESAME, "<filename") # (same thing, explicitly)
    open(SESAME, ">filename") # create file and write to it
    open(SESAME, ">>filename") # append to existing file
    open(SESAME, "| output-pipe-command") # set up an output filter
    open(SESAME, "input-pipe-command |") # set up an input filter

    ... umm but I still don't know to use this.. a little help please?


    -tks
    seth
    seth, Jun 10, 2005
    #2
    1. Advertising

  3. seth

    Paul Lalli Guest

    seth wrote:
    > okay so I didn't look carefully I did find the following examples in
    > the "FileHandle" section of the "programming perl" book
    >
    > open(SESAME, "filename") # read from existing file
    > open(SESAME, "<filename") # (same thing, explicitly)
    > open(SESAME, ">filename") # create file and write to it
    > open(SESAME, ">>filename") # append to existing file
    > open(SESAME, "| output-pipe-command") # set up an output filter
    > open(SESAME, "input-pipe-command |") # set up an input filter
    >
    > .. umm but I still don't know to use this.. a little help please?


    Seth,

    You may want to check out some of the built in perl documentation:
    perldoc -f open
    perldoc perlopentut

    In the mean time, here are two quick examples that show how pipes are
    used both for output and input:

    #!/usr/bin/perl
    use strict;
    use warnings;

    open my $input, 'ls -al |' or die "Cannot do ls: $!";
    #equivalent:
    #open my $input, '-|', 'ls -al' or die "Cannot do ls: $!";
    while (my $line = <$input>){
    print $line;
    }
    close $input;

    open my $output, '| grep foo' or die "Cannot start grep: $!";
    #equivalent:
    #open my $output, '|-', 'grep foo' or die "Cannot start grep: $!";
    print $output "foo bar baz\n";
    print $output "hello world\n";
    print $output "I want food\n";
    close $output;

    __END__


    In the first block, we open the 'ls' command (with -al as parameters),
    and pipe its output to the $input handle. We can then from that handle
    as though it was any other ordinary file handle opened for reading.
    When ls has given us all of its output, the read returns a false value,
    and the while loop terminates.

    In the second block, we open the 'grep' command (with 'foo' as a
    parameter), and pipe its input from the $output handle. We can then
    print to $output as though it were any other ordinary file handle
    opened for writing. Once we have printed all the text we want to be
    used as input to 'grep', we close the file handle, and the 'grep'
    program then runs, displaying its output on the terminal (because we
    didn't redirect it anywhere else.

    Hope this helps,
    Paul Lalli
    Paul Lalli, Jun 10, 2005
    #3
  4. seth

    Guest

    "seth" <> wrote:
    > okay so I didn't look carefully I did find the following examples in
    > the "FileHandle" section of the "programming perl" book
    >
    > open(SESAME, "filename") # read from existing file
    > open(SESAME, "<filename") # (same thing, explicitly)
    > open(SESAME, ">filename") # create file and write to it
    > open(SESAME, ">>filename") # append to existing file
    > open(SESAME, "| output-pipe-command") # set up an output filter
    > open(SESAME, "input-pipe-command |") # set up an input filter
    >
    > .. umm but I still don't know to use this.. a little help please?


    Is the problem that you don't know how to use, or that you don't know why
    you would want to use it?


    open my $fh, "|gzip > zipped_output.gz" or die $!;
    while (whatever()) {
    #stuff goes here;
    print $fh $my_output;
    };

    close $fh or die $!;

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 10, 2005
    #4
  5. seth <> wrote:

    > My question is about pips.



    It is actually about the open() function.

    It appears that you have not read the documentation for the function
    that you are using, that should have been the *first* place you looked.

    Posting to Usenet should be the *last* place you look. Why take the
    time of hundreds/thousands of your fellow programmers when a few
    minutes of docs searching can solve the problem?


    > my @failureMsg;



    You should limit the scope of variables to the smallest possible scope,
    so delete that line and add "my " in front of your 1st usage of @failureMsg.


    > open(OUTPUT, "$command |") or die "can't do the command\n";


    perldoc -f open

    ...
    if the filename ends with a '|', the filename is interpreted
    as a command which pipes output to us


    So the OUTPUT filehandle is open for *reading* by your Perl program.

    You are doing input on an fh named output, that could be confusing,
    "LS_AL" would have been a much more meaningful choice of filehandle name.


    > my $lines = <OUTPUT>;



    Here you read a _single_ line into a variable with a _plural_ name, that
    could be confusing too.


    > @failureMsg = grep(/\ssp.*\.pl$/, <OUTPUT>);


    > print "@failureMsg";



    What does the output from that statement look like?

    Is there an extra space at the beginning of every line except the first?

    If so,

    perldoc -q spaces

    Why do I get weird spaces when I print an array of lines?


    > What I don't understand is the following syntax:


    > open(OUTPUT, " | $command") or die "can't do the command\n";



    perldoc -f open

    If the filename begins with C<'|'>, the filename is interpreted
    as a command to which output is to be piped
    ...

    So the OUTPUT filehandle is open for *writing* by your Perl program.


    > Can someone point me to a quick simple example.


    perldoc -f open

    ...
    See perlipc/"Using open() for IPC" for more examples of this.

    perlipc.pod shows code using pipe opens in each direction.


    > I've checked most of
    > the Orielly series



    The first place to look for the behavior of a function is in the docs
    that came with the software that you are using.

    Books are nice backups, but they are not the primary resource.

    Books are out of date before they even get to the bookstore.

    Perl's std docs are updated each time perl itself is updated.


    > and can't find an example to help me understand what
    > the meaning is for prepnding "$command" with the pipe char.



    Read the documentation for the function if you want to know what
    the function is supposed to do.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 10, 2005
    #5
  6. seth

    seth Guest

    O this 2nd example with the output handle is so slick.. This is exacly
    what I wanted.

    So then with a output handle I can pass args to the command grep - in
    this case. With the 3 examples you've given there will only be two
    match with the first and the second print that you do.
    seth, Jun 11, 2005
    #6
  7. seth

    seth Guest

    The problem is that I don't know how / where I should use the outbound
    pipe to a handle. The examples provided here.. really help....

    tks
    seth
    seth, Jun 11, 2005
    #7
  8. seth

    seth Guest

    Oh boy a lecture.. I really am sorry to take up time from the people
    that responded. But I did spend time looking, where I thought were the
    right places for accurate info to address my confusion.

    However I do appreciate *ALL* the comments here. So I will make more
    of an effort to search the perldocs first next time around.

    -thanks everyone

    seth
    seth, Jun 11, 2005
    #8
  9. seth <> wrote:

    > Oh boy a lecture.



    So long!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 11, 2005
    #9
    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. bernd wegener

    Piping ping into perl-prog

    bernd wegener, Sep 15, 2004, in forum: Perl
    Replies:
    3
    Views:
    654
    Jim Gibson
    Sep 22, 2004
  2. rajnish

    c++ named pipe example

    rajnish, Oct 22, 2004, in forum: C++
    Replies:
    5
    Views:
    19,413
    John Harrison
    Oct 25, 2004
  3. lee, wonsun
    Replies:
    1
    Views:
    484
    Jack Klein
    Nov 2, 2004
  4. Replies:
    1
    Views:
    220
    Ben Morrow
    Jun 2, 2004
  5. Bill H

    Aspell, perl and piping

    Bill H, Mar 26, 2008, in forum: Perl Misc
    Replies:
    1
    Views:
    113
    Ben Morrow
    Mar 27, 2008
Loading...

Share This Page