Why did you change my line:

my @l = `ls -1b`;

to:

my @l = system(`ls -1b`);

? Mine runs the ls command in a subshell and returns the results to the @l array, one line per array element. Yours runs ls in a subshell and captures the output, but then passes that output to system, which tries to run that in another sub-process, passing the return value of that sub-process to @l, making it the first (and only) element. Odds are very good that a listing of the files in your directory doesn't make a valid system command (and could possibly make a harmful one), so the return value is an error number, in this case, -1, 'failed to execute'.

See perlipc and other docs on the difference between system and backticks, as well as other ways to run a sub-process and capture the output, like opening a pipe:

open my $pipe, '-|', 'ls -1b' or die $!; while(<$pipe>){ chomp; say qq| <tag attr='value'>$_</tag> |; } close $pipe;

There are a lot of different ways to do this. Perl isn't called a glue language for nothing. But some are usually better than others, depending on the circumstances.

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re^3: Handling the output of a system command -- how to best manage? by aaron_baugher
in thread Handling the output of a system command -- how to best manage? by taint

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.