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

In reply to Re^5: exec always invokes the shell? win32 by ELISHEVA
in thread exec always invokes the shell? win32 by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.