Ras has asked for the wisdom of the Perl Monks concerning the following question:

What is the advantage of using system call
over using back quotes to kick off a task?
Thanks in advance.

Replies are listed 'Best First'.
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'

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.
      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
        The Perl Cookbook claims (in the Discussion 16.1) that there is overhead associated with the backticks that does not exist with the system call. I imagine perl does the openpipe/fork combination to get the STDOUT from the backticks; if you don't need the output (as in the tar example above), it is thus more efficient to use system.

        -Ton
        -----
        Be bloody, bold, and resolute; laugh to scorn
        The power of man...

        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
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.
      I think it's almost always better to use the perl builtin or a module than a system call. First, it makes your code more portable. What if the script needs to be run on an OS without a "mv" command? Second, it's probably a bit easier to do error handling when calling a perlfunc than having to trap (and sometimes fiddle with) the return value of system. Given the plethora of great modules such as Archive::Tar, File::Copy, Compress::Zlib, Archive::Zip, etc. I find that I very rarely need to do a system call. There are times when you need to call an OS specific or some other 3rd party program but I find those times to be very rare.

      Whenever I find myself using system I always ask myself "Self, is there an all-perl way to do this?" It's saved me time more than once when I had to port the script to a different OS.

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"