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