Search code examples
perlstatactivestate

Unexpected output from perl script


The following script produces no output:

use File::stat;
use Time::localtime;
my $filename = 'c:\testfile';
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
              $atime,$mtime,$ctime,$blksize,$blocks)
                  = stat($filename);
print("$mtime");

c:\testfile exists.

I've seen several answers on SO -- this, for example -- which seem to suggest that the array returned by stat() should have something meaningful in it, but I haven't seen that to be the case in practice.

This is 64 bit ActivePerl on Windows 7.

Does stat not do what those answers seemed to imply, or do Perl's file date/time functions not work under Windows (or 64 bit Windows, or some such?)


Solution

  • This works fine:

    #!perl
    
    use strict;
    use warnings;
    
    my $filename = 'c:\Users\username\Documents\asdf23rasdf.pl';
    my ($dev,  $ino,   $mode,  $nlink, $uid,     $gid, $rdev,
        $size, $atime, $mtime, $ctime, $blksize, $blocks
    ) = stat($filename);
    print($mtime);
    

    As alluded to in the comments - Perl's built-in stat works like the above. You don't need to use File::Stat or File::stat in order to do that. They just provide different interfaces to the same functionality.

    If you want to do it with File::stat it goes like this:

    use File::stat;
    
    my $filename = 'c:\Users\username\Documents\asdf23rasdf.pl';
    my $stats = stat($filename);
    print( $stats -> mtime);