Hello, I have following code that I am using to get modification time of a file. But it is not working. Whether I use stat command or -M operator, I am getting error messages like "Use of uninitialized value…" or "Can't call method "mtime" on an undefined value" depending on which method I use. Any suggestions? I am using MAC OS v10.8.5. I swear that the -M option worked yesterday few times but since then it has stopped working. I am flummoxed.

#!/usr/bin/perl use POSIX qw(strftime); use Time::Local; use Time::localtime; use File::stat; use warnings; $CosMovFolder = '/Logs/Movies'; # sorting files based on modification date opendir (DIR, $CosMovFolder); @moviedir=readdir(DIR); #$file1modtime = -M $moviedir[1]; # tried this, not working. sam +e uninitialized value error message closedir(DIR); @moviedir = sort { -M "$CosMovFolder/$a" <=> -M "$CosMovFolder/$b +" } (@moviedir); #sorting files by modification dates $latestfile = $moviedir[1]; print "file is: $latestfile\n"; open (FH,$latestfile); #$diff_mins = (stat($latestfile))[9]; #didn't work, same uninit +ialized value error message my $diff_mins = (stat(FH)->mtime); # Can't call method "mtime" o +n an undefined value error message print $diff_mins,"\n"; close FH;

In reply to 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.