opening multiple pipes

Discussion in 'Perl Misc' started by justme, Jun 4, 2004.

  1. justme

    justme Guest

    hi

    in unix, we can use pipes such as
    #ls -ltr |grep "txt" | wc -l

    I have looked at IPC::Open2 and IPC::Open3 but haven't a clue how to do it...
    Is IPC::Open2 and IPC::Open3 the correct modules to use for this kind of multiple
    pipes? Would appreciate an example on the above can be implemented in perl..
    thanks very much
     
    justme, Jun 4, 2004
    #1
    1. Advertising

  2. justme

    Bob Walton Guest

    justme wrote:

    > hi
    >
    > in unix, we can use pipes such as
    > #ls -ltr |grep "txt" | wc -l
    >
    > I have looked at IPC::Open2 and IPC::Open3 but haven't a clue how to do it...
    > Is IPC::Open2 and IPC::Open3 the correct modules to use for this kind of multiple
    > pipes? Would appreciate an example on the above can be implemented in perl..
    > thanks very much
    >



    You don't need open2 or open3 to do a one-directional pipe. Try [untested]:


    open PIPE,'ls -ltr |grep "txt" | wc -l|' or
    die "Couldn't open pipe, $!";
    while(<PIPE>){print}
    close PIPE;

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 4, 2004
    #2
    1. Advertising

  3. justme

    Ben Morrow Guest

    Quoth (justme):
    >
    > in unix, we can use pipes such as
    > #ls -ltr |grep "txt" | wc -l
    >
    > I have looked at IPC::Open2 and IPC::Open3 but haven't a clue how to do it...
    > Is IPC::Open2 and IPC::Open3 the correct modules to use for this kind of multiple
    > pipes? Would appreciate an example on the above can be implemented in perl..


    There are many ways to do this in perl. The simplest is to use perl's
    magic open and the shell:

    open my $WC, 'ls -ltr | grep "txt" | wc -l |'
    or die "can't open pipe: $!";

    $WC is now opened for reading from the pipeline. Obviously you can go
    the other way:

    open my $GZIP, '| gzip > out.gz' or die ....;

    and get a FH open for writing.

    IPC::Open{2,3} are for when you need both ends of the program: say
    you've opened something like bc and you are sending questions and
    getting responses (the fact that I can't actually think of a useful
    example that wouldn't be better done 'neat' in Perl shows how seldom you
    need to do this... :). You need to be very careful about deadlocks: read
    the docs.

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
     
    Ben Morrow, Jun 4, 2004
    #3
  4. On Fri, 04 Jun 2004 03:10:13 GMT, Bob Walton
    <> wrote:

    >> in unix, we can use pipes such as
    >> #ls -ltr |grep "txt" | wc -l

    [snip]
    >You don't need open2 or open3 to do a one-directional pipe. Try [untested]:


    Also, I hope that the OP chose that cmd line just as an example, but
    maybe it could be worth pointing out that most often calling commands
    like that from perl is plainly not the best thing to do.

    For example wc -l just counts lines (so btw -ltr are plainly useless -
    this is not perl-related, anyway), so supposing that 'ls|' has been
    open()ed as $fh, one would just need.

    my $cnt=grep /txt/, <$fh>;

    using perl's internal grep() function and scalar context instead.

    But then one could directly use perl's own glob()bing facilities as in

    my $cnt=grep /txt/, <*>;

    or, much better,

    my $cnt=()=<*txt*>;

    For the OP: please note, in case you don't know, that despite the
    visual similarity the <> constructs in <$fh> and <*> (and <*txt*>)
    respectively really represent two *completely* different operators!

    The latter one is a much better approach in shell progamming too,
    IMHO: while not really "forbidden" a cmd line like

    # ls -ltr | grep "txt"

    is indeed awkward: one would normally use

    # ls -ltr *txt*

    also in that context.

    Last, I don't think it makes a substantial difference, but just to
    prevent others from pointing out so, it must be noted that ls by
    default doesn't show files =~ /^\./, so if you *do* want that
    behaviour you may use something like

    my $cnt=grep !/^\./, <*txt*>;


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Jun 4, 2004
    #4
    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. jeremy redburn
    Replies:
    3
    Views:
    3,166
    jeremy redburn
    Nov 25, 2003
  2. Multiple pipes

    , Sep 26, 2007, in forum: C Programming
    Replies:
    3
    Views:
    1,143
    William Pursell
    Sep 30, 2007
  3. fniles
    Replies:
    0
    Views:
    301
    fniles
    Apr 26, 2009
  4. john
    Replies:
    7
    Views:
    256
    Brian McCauley
    Mar 4, 2005
  5. neurino

    Flushing and multiple pipes

    neurino, Jul 20, 2012, in forum: Perl Misc
    Replies:
    2
    Views:
    438
    neurino
    Jul 20, 2012
Loading...

Share This Page