perl; Trying to get File::stat to work

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

perl; Trying to get File::stat to work

martin McCormick-3
I am a member of a perl discussion list but it seems to have gone
away so I hope somebody here can give me an idea as to why the
stat function is not working.

        Create a file called testfile in your working directory
and then run the following perl script:

#!/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);

        As this stands, it should print a 10 or so digit number
representing the number of seconds since Midnight UTC on January
1 of 1970.  What it actually does is to not set the variable and
you get the "Use of uninitialized variable" squawk with no
value assignment to the variable.

        The [9] referrs to the ninth element in an array which
should be the time stamp.

Thanks for any suggestions.

        Martin McCormick WB5AGZ

Reply | Threaded
Open this post in threaded view
|

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

Bob McGowan

It looks like this has to do with mixing the usage of the "native" stat of Perl with the "object" version from File::stat.

The 'stat' from File::stat returns a reference to an object, which has the stuff you're wanting, tucked away internally as object variables.  You need to do:

    use File::stat;

    $statRef = stat('testfile');

    $mtime = $statRef->mtime ()

Hoping this helps.

Bob

On 10/19/18 7:47 PM, Martin McCormick wrote:
I am a member of a perl discussion list but it seems to have gone
away so I hope somebody here can give me an idea as to why the
stat function is not working.

	Create a file called testfile in your working directory
and then run the following perl script:

#!/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);

	As this stands, it should print a 10 or so digit number
representing the number of seconds since Midnight UTC on January
1 of 1970.  What it actually does is to not set the variable and
you get the "Use of uninitialized variable" squawk with no
value assignment to the variable.

	The [9] referrs to the ninth element in an array which
should be the time stamp.

Thanks for any suggestions.

	Martin McCormick WB5AGZ

Reply | Threaded
Open this post in threaded view
|

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

Dave Sherohman-3
In reply to this post by martin McCormick-3
On Fri, Oct 19, 2018 at 09:47:11PM -0500, Martin McCormick wrote:
> I am a member of a perl discussion list but it seems to have gone
> away

Your actual question has already been answered, but, for future
reference, PerlMonks (https://www.perlmonks.org/) is still around and
there are also several Perl folks who frequent StackOverflow
(https://stackoverflow.com/), so you can generally get good answers to
Perl questions pretty quickly from either of those sites.

--
Dave Sherohman

Reply | Threaded
Open this post in threaded view
|

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

martin McCormick-3
In reply to this post by Bob McGowan
Bob McGowan <[hidden email]> writes:

> It looks like this has to do with mixing the usage of the "native" stat of
> Perl with the "object" version from File::stat.
>
>
>
> The 'stat' from File::stat returns a reference to an object, which has the
> stuff you're wanting, tucked away internally as object variables.  You
> need
> to do:
>
>
>     use File::stat;
>
>     $statRef = stat('testfile');
>
>     $mtime = $statRef->mtime ()
>
> Hoping this helps.
>
> Bob

Thank you and thanks to Dave Sherohman <[hidden email]>  He
wrote:

> Your actual question has already been answered, but, for future
> reference, PerlMonks (https://www.perlmonks.org/) is still around and
> there are also several Perl folks who frequent StackOverflow
> (https://stackoverflow.com/), so you can generally get good answers to
> Perl questions pretty quickly from either of those sites.

        It worked perfectly plus I now much better understand how
the pieces all fit together.

Martin

Reply | Threaded
Open this post in threaded view
|

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

David Christensen
In reply to this post by martin McCormick-3
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