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


In reply to Re: Getting modification time in perl not working on my MAC by kcott
in thread Getting modification time in perl not working on my MAC by hary536

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.