Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 81087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:24:16+00:00 2026-05-10T21:24:16+00:00

Is there an elegant way in Perl to find the newest file in a

  • 0

Is there an elegant way in Perl to find the newest file in a directory (newest by modification date)?

What I have so far is searching for the files I need, and for each one get it’s modification time, push into an array containing the filename, modification time, then sort it.

There must be a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:24:16+00:00Added an answer on May 10, 2026 at 9:24 pm

    Your way is the ‘right’ way if you need a sorted list (and not just the first, see Brian’s answer for that). If you don’t fancy writing that code yourself, use this

    use File::DirList; my @list = File::DirList::list('.', 'M'); 

    Personally I wouldn’t go with the ls -t method – that involves forking another program and it’s not portable. Hardly what I’d call ‘elegant’!


    Regarding rjray’s solution hand coded solution, I’d change it slightly:

    opendir(my $DH, $DIR) or die 'Error opening $DIR: $!'; my @files = map { [ stat '$DIR/$_', $_ ] } grep(! /^\.\.?$/, readdir($DH)); closedir($DH);  sub rev_by_date { $b->[9] <=> $a->[9] } my @sorted_files = sort rev_by_date @files; 

    After this, @sorted_files contains the sorted list, where the 0th element is the newest file, and each element itself contains a reference to the results of stat, with the filename itself in the last element:

    my @newest = @{$sorted_files[0]}; my $name = pop(@newest); 

    The advantage of this is that it’s easier to change the sorting method later, if desired.


    EDIT: here’s an easier-to-read (but longer) version of the directory scan, which also ensures that only plain files are added to the listing:

    my @files; opendir(my $DH, $DIR) or die 'Error opening $DIR: $!'; while (defined (my $file = readdir($DH))) {   my $path = $DIR . '/' . $file;   next unless (-f $path);           # ignore non-files - automatically does . and ..   push(@files, [ stat(_), $path ]); # re-uses the stat results from '-f' } closedir($DH); 

    NB: the test for defined() on the result of readdir() is because a file called ‘0’ would cause the loop to fail if you only test for if (my $file = readdir($DH))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's OK to throw any kind of exception (even custom… May 11, 2026 at 9:00 am
  • added an answer I don't believe to-many relationships are supported for automatic KVO… May 11, 2026 at 9:00 am
  • added an answer sort -k5,5 will do the sort on fields and avoid… May 11, 2026 at 9:00 am

Related Questions

Is there an elegant way in Perl to find the newest file in a
Is there an elegant way to create and initialize a const std::vector<const T> like
Is there an elegant way to handle exceptions that are thrown in finally block?
Is there an elegant way to have performant, natural sorting in a MySQL database?
In Java, is there an elegant way to detect if an exception occurred prior
Is there an elegant way to specialize a template based on one of its
I was just wondering if there is an elegant way to set the maximum
Is there some elegant way to add an empty option to a DropDownList bound
Is there an elegant solution for synchronizing sessions across multiple ASP.NET applications? I'm sure
In c# (3.0 or 3.5, so we can use lambdas), is there an elegant

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.