Web lists-archives.com

Re: perl; Trying to get File::stat to work




On 10/19/18 7:47 PM, Martin McCormick wrote:
#!/usr/bin/perl -w
use strict;
use warnings::unused;
use File::stat;
use File::Spec;

my $last_update_time;

$last_update_time = ( stat("testfile") )[9];
printf("%d\n",$last_update_time);

My system:

2018-10-20 20:59:53 dpchrist@vstretch ~/sandbox/perl
$ cat /etc/debian_version
9.5

2018-10-20 21:00:10 dpchrist@vstretch ~/sandbox/perl
$ perl -v

This is perl 5, version 24, subversion 1 (v5.24.1) built for x86_64-linux-gnu-thread-multi
(with 81 registered patches, see perl -V for more detail)

Copyright 1987-2017, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


Create a test file:

2018-10-20 20:59:22 dpchrist@vstretch ~/sandbox/perl
$ touch testfile


Your program:

2018-10-20 20:57:38 dpchrist@vstretch ~/sandbox/perl
$ vi File-stat.pl

#!/usr/bin/perl -w
use strict;
use warnings::unused;
use File::stat;
use File::Spec;

my $last_update_time;

$last_update_time = ( stat("testfile") )[9];
printf("%d\n",$last_update_time);


Compile it:

2018-10-20 21:00:26 dpchrist@vstretch ~/sandbox/perl
$ perl -c File-stat.pl
Can't locate warnings/unused.pm in @INC (you may need to install the warnings::unused module) (@INC contains: /home/dpchrist/perl5/lib/perl5/5.24.1/x86_64-linux-gnu-thread-multi /home/dpchrist/perl5/lib/perl5/5.24.1 /home/dpchrist/perl5/lib/perl5/x86_64-linux-gnu-thread-multi /home/dpchrist/perl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at File-stat.pl line 3.
BEGIN failed--compilation aborted at File-stat.pl line 3.


The Perl binary is installed in different locations on different systems. Revise shebang line to something that is supposed to be more portable.


Leave off '-w' option on shebang line.  'use warnings' normally:


2018-10-20 21:00:57 dpchrist@vstretch ~/sandbox/perl
$ vi File-stat.pl

#!/usr/bin/env perl
use strict;
#use warnings::unused;
use warnings;
use File::stat;
use File::Spec;

my $last_update_time;

$last_update_time = ( stat("testfile") )[9];
printf("%d\n",$last_update_time);


Compile:

2018-10-20 21:01:48 dpchrist@vstretch ~/sandbox/perl
$ perl -c File-stat.pl
File-stat.pl syntax OK


Run:

2018-10-20 21:01:58 dpchrist@vstretch ~/sandbox/perl
$ perl File-stat.pl
Use of uninitialized value $last_update_time in printf at File-stat.pl line 11.
0


So, the error is reproducible.


RFTM File::stat. Note that File::stat overloads Perl's core stat() function (becomes File::stat object constructor). 'stat' now returns an object, not a list.


Use Data::Dumper to see what's going on.


File::Spec is unused -- comment it out.


Use object-oriented attribute accessor method 'mtime' to obtain file modification time.


2018-10-20 21:04:58 dpchrist@vstretch ~/sandbox/perl
$ vi File-stat.pl

#!/usr/bin/env perl
use strict;
#use warnings::unused;
use warnings;
use Data::Dumper;
use File::stat;
#use File::Spec;

# my $last_update_time;

#$last_update_time = ( stat("testfile") )[9];
#printf("%d\n",$last_update_time);

my $f = "testfile";

my $st = stat($f) or die "File::stat constructor failed: $!";
print Data::Dumper->Dump([$st], [qw(st)]);

printf("%d\n", $st->mtime);

2018-10-20 21:16:11 dpchrist@vstretch ~/sandbox/perl
$ perl -c File-stat.pl
File-stat.pl syntax OK

2018-10-20 21:17:22 dpchrist@vstretch ~/sandbox/perl
$ perl File-stat.pl
$st = bless( [
               20,
               970783,
               33188,
               1,
               13250,
               13250,
               0,
               0,
               1540094388,
               1540094388,
               1540094388,
               4096,
               0
             ], 'File::stat' );
1540094388


David