Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: find the latest created file in a directory

by Tux (Monsignor)
on Apr 24, 2013 at 13:57 UTC ( #1030410=note: print w/ replies, xml ) Need Help??


in reply to find the latest created file in a directory

use the CORE:: prefix:

opendir my $dh, $DIR or die "Error opening $DIR: $!"; my @files = sort { $b->[10] <=> $a->[10] } map {[ $_, CORE::stat "$DIR/$_" ]} grep !m/^\.\.?$/, readdir $dh; closedir $dh; my ($name, @stat) = @{$files[0]};

Enjoy, Have FUN! H.Merijn


Comment on Re: find the latest created file in a directory
Select or Download Code
Re^2: find the latest created file in a directory
by Krambambuli (Deacon) on Apr 24, 2013 at 14:59 UTC
    I guess you haven't actually tried your code... :)

    What about
    sort { $b->[1]->[10] <=> $a->[1]->[10] }
    instead of your sort ...?

      Actually, I did:

      $ cat test.pl #!/pro/bin/perl use 5.016; use warnings; my $DIR = "usr"; opendir my $dh, $DIR or die "Error opening $DIR: $!"; my @files = sort { $b->[10] <=> $a->[10] } map {[ $_, CORE::stat "$DIR/$_" ]} grep !m/^\.\.?$/, readdir $dh; closedir $dh; use DP;DDumper $files[0]; $ ls -lrt usr total 16 -rw-rw-rw- 1 merijn users 2588 Jun 18 2004 Honda.gif -rw-rw-rw- 1 merijn users 2388 Feb 20 2007 Honda.png -rw-rw-rw- 1 merijn users 3774 Feb 20 2007 Honda.ico drwxrwxrwx 2 merijn users 4096 Oct 22 2012 terminfo $ perl test.pl [ 'terminfo', 2065, 15481507, 16895, 2, 203, 100, 0, 4096, '1366762203', '1350940900', '1350940900', 4096, 8 ] $ touch usr/new $ perl test.pl [ 'new', 2065, 11278617, 33206, 1, 203, 100, 0, 0, '1366816276', '1366816276', '1366816276', 4096, 0 ] $

      Enjoy, Have FUN! H.Merijn
        Oh... sorry, got it now.

        I forgot that Core::stat returns a list (while File::stat returns a reference to a File::stat object).

        Thanks for clarifying.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2015-07-07 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls