mr19 has asked for the wisdom of the Perl Monks concerning the following question:

Im pretty new to scripting with perl and i have a problem with stat and i don't understand why its not working..

The error is: Use of uninitialized value $mtime in string at test.pl line 10.

The error shown points to print.

#!/usr/local/bin/perl -w use strict; use warnings; use File::stat; my $filename = "test.txt"; my $mtime = (stat $filename)[9]; print $mtime;

I just wanted to simply display the timestamp of that file

Thanks in advance

Michael

Replies are listed 'Best First'.
Re: Problem with file::stat
by Anonymous Monk on Jun 11, 2015 at 12:08 UTC
    Perl has two stats: a built-in one, and the one in File::stat.
    This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. - File::stat
    So just remove the line use File::stat, or access the mtime like this: (stat $filename)->mtime.

      Hello, thanks to all three of you for the fast response.

      Without the File::stat it works like a charm.

      Now i can continue testing.

      best regards

      Michael
Re: Problem with file::stat
by Discipulus (Canon) on Jun 11, 2015 at 12:12 UTC
    dont use the module, use the stat function directly.
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Problem with file::stat
by thanos1983 (Parson) on Jun 11, 2015 at 12:24 UTC

    Hello mr19,

    The monks have already answered your question. But you can also pass stat values to an array:

    Sample:

    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $filename = 'test.txt'; my @array = stat($filename); print Dumper \@array; my $epoch_timestamp = (stat($filename))[9]; print "Epoch seconds: " . $epoch_timestamp . "\n"; __DATA__ $VAR1 = [ 64513, 7741435, 33188, 1, 283683, 6856, 0, 0, 1434023984, 1434023978, 1434023983, 4096, 0 ]; Epoch seconds: 1434023978

    Or if you want to use the module:

    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $filename = 'test.txt'; my @array = stat($filename); print Dumper \@array; my $epoch_timestamp = (stat($filename))[9]; my $timestamp = localtime($epoch_timestamp); print "\nEpoch seconds: " . $epoch_timestamp . "\n"; print "Date and time: " . $timestamp . "\n"; statSubRoutine(); sub statSubRoutine { use File::stat; my $timestamp = (stat($filename)->mtime); my $localTimeStamp = localtime($epoch_timestamp); print "\n\$timestamp: " . $timestamp . "\n"; print "\$localTimeStamp: " . $localTimeStamp . "\n"; } __DATA__ $VAR1 = [ 64513, 7741354, 33188, 1, 283683, 6856, 0, 0, 1434028480, 1434028479, 1434028479, 4096, 0 ]; Epoch seconds: 1434028479 Date and time: Thu Jun 11 15:14:39 2015 $timestamp: 1434028479 $localTimeStamp: Thu Jun 11 15:14:39 2015

    Update: Including conversion epoch seconds to human readable form.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!