in reply to Re^2: Calling xmllint better than using a system()
in thread Calling xmllint better than using a system()

My opinion? Your support programmer needs to do more benchmarking. :-) Using system can be faster in some circumstances. Sometimes, it's a matter of coder's time vs CPU time, especially when the CPU time isn't important. :-)

The above code does not hit the shell because I'm calling system with a list of parameters instead of a single string. When you call system("some stuff here"), perl does not go and split that on the spaces and call "some" with the parameters "stuff", "here". Instead, it passes the whole thing to the shell, and lets the shell do the splitting up. If there are special metacharacters, the shell will act on them. If you don't need the shell to act on things like redirection and piping between subprocesses, this is all just extra overhead. I know, I just said "using system can be faster" - it all depends on what you're doing. If you need the shell to redirect, then it's far easier to let the shell do it than for you to do it yourself. But for most of the time, you're actually creating the string for the shell to parse, you may as well just create the list.

When you call system(@list), perl calls a different POSIX API. Perl will automatically perform a fork and exec, but the exec is a execvp (or similar). This is what the shell itself does under the covers. Since everything is already set up as a list, this call bypasses the shell and goes directly to the executable you want. (Perl is nicer than this - it automatically finds the executable in the PATH if necessary, which is a simplification that is really super handy.)

Normal POSIX system always uses the shell. But perl's system will use POSIX system when it's a single string with either spaces or shell metacharacters, or fork/execvp when it's a list, whether there are spaces/metacharacters or not.

I always advocate using system(@list) just so as to avoid the shell metacharacters having meaning. With the obvious exception of when you really do want those shell metacharacters to do your work for you :-) It's generally just easier. And cheaper on RAM (one less process running) and on CPU (one less process to initialise/tear down).

Basically, there's a difference between running another program and "hitting the shell" (using the shell to run another program). In C, I find most people use the shell because system is so much easier thank fork/execvp. Perl already has done that work, so avoiding the shell becomes so much easier. :-)

  • Comment on Re^3: Calling xmllint better than using a system()

Replies are listed 'Best First'.
Re^4: Calling xmllint better than using a system()
by itsscott (Sexton) on Jul 26, 2011 at 20:33 UTC
    {bowing humbly}

    Thanks for the time you took to explain that, makes a lot of sense. I've actually started to incorporate some Lua into our C code, pretty basic right now, but it is interesting. Would be interesting to have the same kind of hooks Lua has with C in Perl.

    Anyhow, thanks again for the excellent and informative reply.