Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #201457 > unrolled thread

perl; Trying to get File::stat to work

Started by"Martin McCormick" <martin.m@suddenlink.net>
First post2018-10-20 04:50 +0200
Last post2018-10-21 06:40 +0200
Articles 5 — 4 participants

Back to article view | Back to linux.debian.user


Contents

  perl; Trying to get File::stat to work "Martin McCormick" <martin.m@suddenlink.net> - 2018-10-20 04:50 +0200
    Re: perl; Trying to get File::stat to work Bob McGowan <ramjr0915@gmail.com> - 2018-10-20 05:50 +0200
      Re: perl; Trying to get File::stat to work "Martin McCormick" <martin.m@suddenlink.net> - 2018-10-20 14:30 +0200
    Re: perl; Trying to get File::stat to work Dave Sherohman <dave@sherohman.org> - 2018-10-20 10:20 +0200
    Re: perl; Trying to get File::stat to work David Christensen <dpchrist@holgerdanske.com> - 2018-10-21 06:40 +0200

#201457 — perl; Trying to get File::stat to work

From"Martin McCormick" <martin.m@suddenlink.net>
Date2018-10-20 04:50 +0200
Subjectperl; Trying to get File::stat to work
Message-ID<wKPuV-2dg-1@gated-at.bofh.it>
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

[toc] | [next] | [standalone]


#201459

FromBob McGowan <ramjr0915@gmail.com>
Date2018-10-20 05:50 +0200
Message-ID<wKQqZ-2Lu-1@gated-at.bofh.it>
In reply to#201457

[Multipart message — attachments visible in raw view] — view raw

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
>

[toc] | [prev] | [next] | [standalone]


#201479

From"Martin McCormick" <martin.m@suddenlink.net>
Date2018-10-20 14:30 +0200
Message-ID<wKYye-7C8-9@gated-at.bofh.it>
In reply to#201459
Bob McGowan <ramjr0915@gmail.com> 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 <dave@sherohman.org>  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

[toc] | [prev] | [next] | [standalone]


#201464

FromDave Sherohman <dave@sherohman.org>
Date2018-10-20 10:20 +0200
Message-ID<wKUEh-5lZ-1@gated-at.bofh.it>
In reply to#201457
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

[toc] | [prev] | [next] | [standalone]


#201496

FromDavid Christensen <dpchrist@holgerdanske.com>
Date2018-10-21 06:40 +0200
Message-ID<wLdGW-87f-1@gated-at.bofh.it>
In reply to#201457
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

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web