Re: system call vs. back quotes
by Chmrr (Vicar) on Jan 25, 2002 at 02:36 UTC
|
The biggest difference is what each one returns. system returns the return value of the whatever you executed, whereas backticks capture and return whatever said program printed on STDOUT. This is the property which usually determines which you want to use. There are a few other differences, however; the multiple-argument form of system skips the shell, thus eliminating the risk of having to deal with shell metacharacters, for example.
perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'
| [reply] [d/l] |
Re: system call vs. back quotes
by tadman (Prior) on Jan 25, 2002 at 02:57 UTC
|
Also, in addition to what Chmrr said, there is the fact that system is safer than backticks. With system you can specify the executable to be run explicitly, whereas the backticks have it specified implicitly. That is to say that system can be used like backticks, but does not have to be.
# All similar in functionality
`tar czf /tmp/stuff.tar.gz /stuff`
open ("tar czf /tmp/stuff.tar.gz /stuff");
system ("tar czf /tmp/stuff.tar.gz /stuff");
system ("tar", "czf", "/tmp/stuff.tar.gz", "/stuff");
# Don't search path, specify explicitly
system ("/bin/tar", "czf", "/tmp/stuff.tar.gz", "/stuff");
Using the system call is also useful when you want to control how the parameters are split. It is a more flexible way of running programs than either open or backticks. | [reply] [d/l] |
|
|
Perhaps I'm missing something obvious here. Could you explain to me how this:
system ("/bin/tar", "czf", "/tmp/stuff.tar.gz", "/stuff");
is different from this:
`/bin/tar czf /tmp/stuff.tar.gz /stuff`
Is there some inherent advantage to the system call other than those listed by Chmrr above?
Thanks for the clarification.
Vavoom | [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
The 'system' call treats its first argument as the command to run and the remaining arguments as arguments to that command. Bypassing the shell means that even if the other arguments contained pipe/redirect characters or other commands, those redirects, commands, etc. will not processed. That helps to eliminate a lot of security problems that can be caused by tainted data.
(Of course, that's not to say that you shouldn't run in taint mode and untaint your data anyway.)
Impossible Robot
| [reply] |
Re: system call vs. back quotes
by indapa (Monk) on Jan 25, 2002 at 03:21 UTC
|
A follow up question on this thread. Is it better to use
backticks/system calls rather than a perl function. For example
when renaming files is better to use perl's rename function,
or a 'mv' command? Or another example, using unlink vs. rm. | [reply] |
|
|
| [reply] |
Re: system call vs. back quotes
by strat (Canon) on Jan 25, 2002 at 20:18 UTC
|
I prefer using open-PIPE to backticks because you easier get errors, e.g.
unless (open (CMD, "$anycommand |")){
die "Error in executing $anycommand: $!\n";
}
else {
my @result = <CMD>;
close (CMD);
}
I always try to use perl internal functions if possible. Only if there are very important reasons, I use operating system commands, e.g. $anycommand = "dir /b /s *.doc"; or the like instead of File::Recurse, because it is faster.
Best regards,
perl -e "$_=*F=>y~\*martinF~stronat~=>s~<A HREF="/index.pl?node=%5E%5Cw&lastnode_id=1072">^\w</A>~~g=>chop,print" | [reply] [d/l] [select] |