Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Getting modification time in perl not working on my MAC

by hary536 (Novice)
on Mar 21, 2014 at 14:52 UTC ( #1079278=perlquestion: print w/ replies, xml ) Need Help??
hary536 has asked for the wisdom of the Perl Monks concerning the following question:

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;

Comment on Getting modification time in perl not working on my MAC
Download Code
Re: Getting modification time in perl not working on my MAC (because i'm a stupid noob)
by Anonymous Monk on Mar 21, 2014 at 15:02 UTC

     -M $dir[1]; # tried this, not working.

    Non-existent variables don't exist ... makes sense to me

    Whatever problem you're trying to solve, use strict 'vars'; will warn you about inventing variable names you havent used yet

    Anyway, readdir is the devil, use Path::Tiny, it throws "exceptions" like autodie

    use Path::Tiny qw/ path /; my @files = path( $CosMovFolder )->realpath->children(); my @sorted_files = map { $$_[1] } sort { $$a[0] <=> $$b[0] } map { [ -m $_, $_ ] } @files;
      Hi, Thanks for your reply. I was copying and pasting the main code out of my file here(omitting the extra comments I have in my file). I had modified variable names in my main code but not in the comments. Though, with all the variables correct, it is still not working. -M command is working if it is between the opendir and closedir lines. If -M is outside anywhere it is not working. I am trying to compare the modification time of the latest modified file against the current time and make some decision if the time difference is more than 30 minutes. Any suggestions? Thanks.
        Correction to my previous statement. -M option works as long as that line is before the sort line. IF I use it after the sort line, it gives that uninitialized message. I don't understand why. Thanks.

        Though, with all the variables correct, it is still not working. -M command is working if it is between the opendir and closedir lines

        Well you have  -M $moviedir[1] and  -M "$CosMovFolder/$moviedir[1]" its the basic stumbling block of readdir that everyone stumbles on .

        readdir is just low level api ... low level api are a pain

        use Path::Tiny its easy

        use Path::Tiny qw/ path /; my @sorted_files = map { $$_[1] } sort { $$a[0] <=> $$b[0] } map { [ $_->stat->mtime, $_ ] } path( $CosMovFolder )->realpath->children();
Re: Getting modification time in perl not working on my MAC
by hazylife (Monk) on Mar 21, 2014 at 17:11 UTC
    Two things:
    1. filter out "." and ".."
    2. prepend the directory name:
    @moviedir = map { /^\.\.?$/ ? () : "$CosMovFolder/$_" } readdir(DIR);
      Thanks. Prepending the directory name worked. I totally missed that. Thanks again.
Re: Getting modification time in perl not working on my MAC
by kcott (Abbot) on Mar 21, 2014 at 18:37 UTC

    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

      What is the -M modifier?

        "What is the -M modifier?"

        The short answer is there's no such thing (as far as I know).

        In the context of this thread, '-M' is a file test operator (not a modifier). You'll also see that I've used '-f', which is another file test operator. Here's a full list of "file test operators" with an explanation of each and details of usage.

        There's also the '-M' you can use when running perl. See "perlrun: Command Switches" for details.

        Perhaps you were thinking of the 'm' modifier (which can be used with a regex). See "perlre: Modifiers".

        -- Ken

      Thanks Ken for your detailed reply and explanation. That was helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1079278]
Front-paged by Arunbear
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: (4)
As of 2014-08-29 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls