Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any use in providing arguments as separate parameters to a system call using Perl?

Tags:

perl

On Unix, all these three generate the same result

system("top -H -p $pid -n 1");             #ver1
system("top", "H", "p $pid", "n 1");       #ver2
system("top", "-H", "-p $pid", "-n 1");    #ver3
  • What is the difference between ver2 and ver3?

  • Is there any reason I should use ver2 and ver3, and not ver1?

  • They do not even support piping the results, for example, are there any ver2 and ver3 equivalents of the following call?

    system("top -H -p $pid -n 1 | grep myprocess | wc -l");
    
like image 262
Lazer Avatar asked Oct 04 '10 11:10

Lazer


1 Answers

Even it looks same it is not same:

$ perl -e 'system("./test.pl -H -p $$ -n 1");system("./test.pl", "H", "p $$", "n 1");system("./test.pl", "-H", "-p $$", "-n 1");'
-H,-p,10497,-n,1
H,p 10497,n 1
-H,-p 10497,-n 1
$ cat ./test.pl 
#!/usr/bin/perl
$\="\n";
$,=",";
print @ARGV;

It is up to top implementation that it works same. Other applications may not work same.

like image 64
Hynek -Pichi- Vychodil Avatar answered Sep 29 '22 09:09

Hynek -Pichi- Vychodil