- What is the difference between ver2 and ver3?
Just in what arguments you're passing to top
. I don't know of a version of top
that will take switches without dashes like some versions of ps
do, so you should use version 3.
- Is there any reason I should use ver2 and ver3, and not ver1?
If you pass a single string to system
it will run it via your shell. This means it will be shell interpreted. Any stray spaces or shell meta characters (quotes, dollar signs, etc...) in the arguments would be interpreted and possibly mess things up. It's also a potential security hole.
For example, if $pid
was something like '10; echo pwnd; echo '
then you'd run top -H -p 10
then echo pwnd
then echo -n1
.
So for both safety and security, unless you need shell processing (see below) you should pass system a list.
- Are there any ver2 and ver3 equivalents which allow pipes?
No, piping and redirection is done by the shell. You have to use something other than system
. You can do it with open
, but it's a pain in the ass. Easiest way is to use IPC::Run.
use IPC::Run;
my $out;
run ["echo", "foo\nbar\nbaz"], "|",
["grep", "ba"], "|",
["wc", "-l"],
\$out;
print $out; # 2
But really if you're just grepping and counting a handful of lines, use Perl.
my $out;
run ["echo", "foo\nbar\nbaz"], '>', \$out;
my $count = grep { /ba/ } split /\n/, $out;
print $count;