The "stat" function is implemented in Perl as a direct system call OS library call because it is commonly recognized that starting up and shutting down a whole shell process just to stat a file will tend to be a noticeable waste of run time, especially given the very typical usage (like in the code above) that could end up doing this relatively costly business many many times. (update: likewise for unlink, rename, and similar built-ins)

Plus, /usr/bin/stat tends to be one of those "core utilities" that don't actually have equivalent command-line syntax from one unix/linux OS to the next. (update: it might be /usr/local/bin/stat, or as pointed out above, it might not be found at all on some systems)

To clarify the run-time issue... Just for grins I found a directory on my mac laptop (2.2GHz intel core i7, osx 10.8.5, perl 5.12) that happens to contain 12.6K files, and I tried two different scripts:

$ cat /tmp/j1.pl #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; my $stat = `stat $_`; } $ cat /tmp/j2.pl #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; my $stat = stat $_; } $ cd path/with/12.6K_files/ $ ls | time /tmp/j1.pl 60.05 real 12.76 user 31.45 sys $ ls | time /tmp/j2.pl 0.07 real 0.01 user 0.02 sys
I did it a few times, in both orders (j1 right after j2 and vice-versa), and the results were consistent. Imagine what the difference would be over a whole disk with, say, a million files.

As for the cross-platform portability issue… I have to ask: which OS / version of stat are you using? The version of /usr/bin/stat I have on my mac doesn't support the particular command-line syntax you used, so even if I wanted to run your script, I couldn't.

UPDATE: Oh, I forgot to mention… Have you tried running your script in a directory where file names contain spaces, and/or parens, and/or apostrophes, and/or ampersands, and/or … (I hope you get the idea).


In reply to Re^4: removing files with perl based on age and name by graff
in thread removing files with perl based on age and name by Jocqui

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.