Hi Monks!
I want to build a subroutine which returns the latest modified file in an array of files (could be regular file, link or directory).
The area is given in $base_dir variable and the filenames list hash ref is given in $filenames_href (The keys are the number and the values are just "1").
The initial code:
sub test { my ($base_dir,$filenames_href) = @_; foreach my $filename (keys(%{$filenames_href})) { my $file_path = catfile($base_dir,$filename); # Could be file, + dir, link, ... # TODO: Check if the $file_path is the latest I seen ? } return $latest_modified_file; }
For example:
ls -la . -rw-r----- 1 user group 33659 Nov 3 14:49 a drwxr-x--- 3 user group 4096 May 3 2021 b drwxr-x--- 3 user group 4096 Dec 8 16:18 c -rwxr-x--x 1 user group 67 Apr 5 2021 e drwxr-x--- 4 user group 4096 Nov 1 15:29 f -rw-r----- 1 user group 798 Nov 15 17:00 g
In that case the sub should return c because it was modified latest. Is there a standard way to do it?

In reply to Getting latest modified file in a directory by ovedpo15

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.