http://www.perlmonks.org?node_id=887534


in reply to Re^4: exec always invokes the shell? win32
in thread exec always invokes the shell? win32

The question "why is exec splitting "1 2" into "1" and "2", but system does not is a good question. The conclusion that exec always calls the shell is not.

Quite simply, your theory is counter to the Perl documentation and the intent of both system and exec. Neither exec nor system call the shell except possibly in the case that someone tries to exec a shell built-in. If you want the shell invoked, you use backticks. exec and system use native systems calls whenever possible.

In the case of Ms-Win, exec first tries a direct call on the program via CreateProcess. The shell is only invoked if this calls fails or certain internal Perl limits on the number of wait objects are reached.. Both exec and system use the same code. The only difference between the two is a single mode code (P_WAIT vs. P_NOWAIT). This does not affect parameter handling. You can confirm this yourself by looking at the source code yourself.

As regards parameter mangling. CreateProcess takes a string, not an array, so Perl collapses your arguments into a single string. (Confirmed by source diving). What actually happens after that depends on the how the receiving program chooses to parse the command line string.

As for the differences in their behavior, based on reading the Perl source code, I see two possibilities:

Finally, you should know that not all platforms get the results you did. The behavior I get running your script on a Linux box does not show any difference between system and exec. "1 2" does not split into two parameters (hardly surprising since linux takes an parameter array not a command line string). Also I get the reverse behavior you do: no args has no output, 1 arg has output:

$ perl Anony.pl perl -l -e "print for @ARGV" -- 1 2 system at Anony.pl line 19. exec at Anony.pl line 22. $ perl Anony.pm 1 perl -l -e print for @ARGV 1 2 system at Anony.pl line 19. 1 2 exec at Anony.pl line 22. 1 2