Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > opening multiple pipes

Reply
Thread Tools

opening multiple pipes

 
 
justme
Guest
Posts: n/a
 
      06-04-2004
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
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      06-04-2004
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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-04-2004

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed) (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] (E-Mail Removed)
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      06-04-2004
On Fri, 04 Jun 2004 03:10:13 GMT, Bob Walton
<(E-Mail Removed)> 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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flushing and multiple pipes neurino Perl Misc 2 07-20-2012 01:45 PM
Multiple pipes ZhukovL@gmail.com C Programming 3 09-30-2007 11:37 AM
Anyone resolved links opening blank page? or box opening in Outlook 2000? S.Rodgers Firefox 13 12-16-2005 03:07 PM
perl problem with select and non-blocking sysread from multiple pipes john Perl Misc 7 03-04-2005 02:34 PM
Problem with multiple pipes in script executed from Runtime.exec() jeremy redburn Java 3 11-25-2003 02:41 PM



Advertisments