in reply to system call vs. back quotes

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.

Replies are listed 'Best First'.
Re: Re: system call vs. back quotes
by Vavoom (Scribe) on Jan 25, 2002 at 03:49 UTC
    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 '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
      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...