开发者

What's behind the 'system' function in Perl?

I can thought that it will open a shell, execute the parameter (shell command) and return the result in a scalar. But, execute system function in a Perl script is faster than a shell command. It will call th开发者_StackOverflow社区is command in C? If yes, what's the difference between

rmdir foo

and

system('rmdir foo');


  1. The difference between the two is that the second one will open (fork) a child process (which will be the rmdir command) while the first one will make a direct Unix system call using the API without opening a process. Opening child process is expensive resource wise.

  2. system() call will always open a child process to execute, BUT, it may either open a shell which will in turn fork off the desired program as its own child process (thus resulting in 2 child processes), or fork off the program as a child process directly.

    The choice of when Perl will open a shell during a system() call is spelled out in perldoc -f system. The short (and not 100% accurate) version is:

    • If there is only one parameter to system call, and the parameter evaluates to a string that contains shell meta-characters, a shell will be forked first.

    • If there's only one parameter and it evaluates to no metacharacters; or there's a >1 element list of parameters, the program is forked directly bypassing shell.

Thus:

system("rmdir foo"); # forks off rmdir command directly

system("rmdir", "foo"); # forks off rmdir command directly

system("rmdir foo > out.txt"); # forks off a shell because of ">" 


Your system call starts a separate process while Perl's own rmdir will call the native C function in the context of the Perl process. Both methods end up doing the same system calls, but opening a new process is less efficient.*

It's best practice to use Perl's own functions (such as rmdir): they are portable, and you don't have to worry about interpreting shell exit codes, escaping metacharacters in filenames to prevent security risks, etc.

*system will create an additional sh process if your command contains pipes, input/output redirection, && etc. This does not apply in your case, but can make the system option even slower.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜