If your original "du" command is covering a lot of sibling directories that match "/tmp/dir*", and if each of those has a any amount of subdirectory structure, then I would probably stick with your backtick approach, but it can be very simple:
my @size = `du -sk /tmp/dir* | cut -f1`;
Note that in a list context (e.g. assigning to an array), the backticks will split on line breaks for you, and retain the line-feed at the end of each line (each element of the array).

If the "du" fails there, @size will be empty, but the error report from du will go to your STDERR. If you want to catch the error report, and you are using a bourne-style shell (as opposed to a csh-style shell), you can redirect the shell's stderr, as explained at length in the description of "qx" (backtick opertator) in "perldoc perlop":

my @size = `du -sk /tmp/dir* 2>&1 | cut -f1`;
This will put the error message as the sole element of @size. Then again, if you somehow get warnings on some files/paths, like "permission denied", these will be mixed in with size numbers for paths that had no problems -- which makes @size kind of a mess... So you could redirect the shell's stderr to a file (if you want to read it later from the perl script) or just redirect to /dev/null (if you don't need to read it).

In reply to Re: Capturing shell command error by graff
in thread Capturing shell command error by kulls

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.