You don't need to use the -l (letter ell) option if you just want the filenames. You can use -1 (numeral one) to specify one file per line, but that's the default behavior of ls when output isn't going to a terminal anyway.

Additionally, if you want the most recent files, instead of using -r and getting the last five files, you should use the default sort order and get the first five files. Thus:

my @files = (`ls -1t`)[0..4]; chomp(@files);

However, it might be preferable to do all this in Perl, and avoid spawning a shell and relying on the behavior of ls.

opendir(DIR, '.') or die "Can't opendir '.': $!\n"; my @files = readdir(DIR); closedir(DIR); @files = map $_->[0], sort { $b->[1] <=> $a->[1] } map { /^\./ ? () : [ $_, (lstat $_)[9] ] } @files; @files = @files[0..4];

In reply to Re: Reading from ls -lrt by chipmunk
in thread Reading from ls-lrt (was: Reading) by azatoth

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.