[PLUG] Grab the most recent file in perl?

Jeme A Brelin jeme at brelin.net
Thu Feb 13 15:01:01 UTC 2003


On Thu, 13 Feb 2003, Stafford A. Rau wrote:
> What would be the nicest way in Perl to grab the name of the most recent (by
> modfication time) file in a directory with a bunch of files?
>
> I could do something gross like:
> @files = `/bin/ls -tr /var/local/flows/ft*`;
>
> But no, I shouldn't have to shell out for pretty much anything these
> days.
>
> Would it be more like:
>
> for (</var/local/flows/ft*>) {
> 	$file_hash{$_} = (stat $_)[9];
> }
>
> and then sort by the hash by value? I'm betting there's a better way,
> and am happy to hear suggestions.

Won't this do it?

$newest = (sort { -M $a <=> -M $b } <*> )[0]

J.
-- 
   -----------------
     Jeme A Brelin
    jeme at brelin.net
   -----------------
 [cc] counter-copyright
 http://www.openlaw.org




More information about the PLUG mailing list