Yes, the two forms are different.

In the first form, a system call (eg, the C function as implemented internally by the operating system for executing commands) is made with the arguments provided. In the second form, the string is passed to the shell to deal with. The difficulty that you are running into is with escaping of meta-characters (characters special to the shell, but not special to the internal system call).

The first form (system call) is equivalent to automatically escaping all arguments. So when you say above that the first form "works", it really doesn't, as it just passes ">$output_file" as an argument to the command, which in general won't do what you want. I don't know of any way to do something like piping to a file using the first form (you normally use open() for that instead).

The second form (shell command) probably doesn't work for you because you are not escaping characters that you want escaped. Escaping can be done automatically in Perl using quotemeta() or \Q...\E.

As an example, consider:

>echo two > one

If you want this to echo "two > one", then you need to escape the ">":

system "echo", "two", ">", "one";
system "echo two \\> one";
system "echo " . quotemeta ( "two > one" );
system "echo \Qtwo > one\E";

If you want "two" to be piped to a file called "one", then you don't:

system "echo two > one";



In reply to Re: Syntax details with "system" by thewebsi
in thread Syntax details with "system" 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.