opening multiple pipes

J

justme

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
 
B

Bob Walton

justme said:
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;
 
B

Ben Morrow

Quoth (e-mail address 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
 
M

Michele Dondi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top