@args = ('cmd.exe', '/c', 'erase', $filename);

If Win32 had "real"™ command-line argument processing, then that wouldn't work. For example, in Unix you have to use:

@args = ( 'sh', '-c', "erase $filename" );
and this is important in case you want to quote arguments to be given to the internal command. But going very far down that road gets rather complicated quickly so I'll leave that for another time.

The reason your code works is because, in Win32, system(@args) is (quite unfortunately) exactly the same as system("@args") because Win32 only deals with command lines while Unix (and C) want to deal with lists of command arguments. This can make getting the proper arguments passed in quite difficult, especially since it also means that each program parses its own command line and so you often have to prepare/quote your arguments different for different commands.

Also note that Perl will do the "cmd /c" trick for you automatically (if it can't find a "erase.*" executable in your PATH).

                - tye

In reply to Re^2: Executing DOS shell commands via system() (/c nits) by tye
in thread Executing DOS shell commands via system() by Grygonos

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.