in reply to Getting modification time in perl not working on my MAC
G'day hary536,
Firstly, your problems have nothing to do Mac OS X (the code below was run on Mac OS X 10.7.5).
From the File::stat module's documentation:
"This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects."
So, if you use this module, don't try to access array elements from stat (or lstat) calls.
Array indices are zero-based. So $moviedir[1] is probably not accessing the element you want.
[From the first reply you received, it would appear your OP originally had $dir[1] where it now has $moviedir[1] (although you don't appear to have changed the "not working" comment). Please do not change your OP without clearly indicating what you've changed. See "How do I change/delete my post?" for details.]
You should also consider whether you want to stat all directory entries (e.g. plain files, directories, symbolic links, etc.) or just a subset of these — in the code below I've excluded everything except plain files.
The scripts that follow are only intended to show that the various functions for determining modification time do work. I created this directory for the tests:
$ ls -al pm_1079278_mtime_test total 0 drwxr-xr-x 5 ken staff 170 22 Mar 04:21 . drwxr-xr-x 584 ken staff 19856 22 Mar 04:22 .. -rw-r--r-- 1 ken staff 0 22 Mar 04:19 test1 -rw-r--r-- 1 ken staff 0 22 Mar 04:20 test2 -rw-r--r-- 1 ken staff 0 22 Mar 04:21 test3
Script using core stat:
#!/usr/bin/env perl -l use strict; use warnings; use autodie; my $path = './pm_1079278_mtime_test'; opendir(my $dh, $path); my @files = map { "$path/$_" } readdir $dh; closedir $dh; my @sorted_files = sort { -M $a <=> -M $b } @files; for (@sorted_files) { print '-' x 30; print "File: $_"; next unless -f; print '-M: ', -M; print 'stat9: ', (stat)[9]; }
Output:
------------------------------ File: ./pm_1079278_mtime_test/.. ------------------------------ File: ./pm_1079278_mtime_test/. ------------------------------ File: ./pm_1079278_mtime_test/test3 -M: 0.0440856481481482 stat9: 1395422472 ------------------------------ File: ./pm_1079278_mtime_test/test2 -M: 0.0447916666666667 stat9: 1395422411 ------------------------------ File: ./pm_1079278_mtime_test/test1 -M: 0.0450810185185185 stat9: 1395422386
Script using File::stat:
#!/usr/bin/env perl -l use strict; use warnings; use autodie; use File::stat; my $path = './pm_1079278_mtime_test'; opendir(my $dh, $path); my @files = map { "$path/$_" } readdir $dh; closedir $dh; my @sorted_files = sort { -M $a <=> -M $b } @files; for (@sorted_files) { print '-' x 30; print "File: $_"; next unless -f; print '-M: ', -M; print 'mtime: ', stat($_)->mtime; }
Output:
------------------------------ File: ./pm_1079278_mtime_test/.. ------------------------------ File: ./pm_1079278_mtime_test/. ------------------------------ File: ./pm_1079278_mtime_test/test3 -M: 0.044849537037037 mtime: 1395422472 ------------------------------ File: ./pm_1079278_mtime_test/test2 -M: 0.0455555555555556 mtime: 1395422411 ------------------------------ File: ./pm_1079278_mtime_test/test1 -M: 0.0458449074074074 mtime: 1395422386
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting modification time in perl not working on my MAC
by Anonymous Monk on Mar 26, 2014 at 01:54 UTC | |
|
Re^2: Getting modification time in perl not working on my MAC
by carlriz (Beadle) on Mar 25, 2014 at 17:20 UTC | |
by kcott (Archbishop) on Mar 26, 2014 at 05:25 UTC |