Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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:

  • system did not fail and exec did. Hence one went through cmd.exe and the other did not. There are two possible causes for failure. Inside Perl, there is a maximum limit on the number of wait objects. This affects exec but not system. However, unless your perl is compiled with very low limit (0?) that isn't likely going to be an issue in your script. Outside Perl in the Ms-Windows environment, you'd have to look for some sort of resource allocation problem that affects exec'd processes but not waited upon processes.

  • You are using a perl compiled from a different codebase than the one I'm looking at. The codebase linked to above is the main Perl codebase. However, there is more than one MsWin codebase for Perl (Active State, for instance, uses some of its own proprietary sources). Or perhaps you are working with an older release of an open source version of Perl.

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-08-29 04:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (275 votes), past polls