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

Hi everyone,

I tried making a post about this earlier but I wasn't clear enough and it didn't go so well. Anyway, here's my second attempt ... I'm trying to implement a pretty simple code using 'stat' and 'localtime' as follows:
#!perl use warnings; use strict; use 5.010; open FILE, ">hoser.txt"; my @file_info = stat(FILE); close FILE; print join "\n", @file_info; my $timestamp = pop( @file_info ); print my $date = localtime $timestamp;
Seems simple enough right?? Well, I keep getting the same warning but don't know why:

Argument "" isn't numeric in localtime at examples.pl line 13.
Everything still works but I don't understand what it's complaining about. By works, I mean the localtime operations works but if I try printing the contents of $timestamp for example, I just get the warning.

Help much appreciated.

Replies are listed 'Best First'.
Re: stat and localtime - take2
by repellent (Priest) on Feb 02, 2009 at 01:35 UTC
    According to stat, pop( @file_info ) returns actual number of blocks allocated. You are performing localtime() on an incorrect type of argument. Instead, you may be looking for:
    # last modify time in seconds since the epoch my $timestamp = $file_info[9];
Re: stat and localtime - take2
by linuxer (Curate) on Feb 02, 2009 at 12:20 UTC

    Maybe not really related to your question, but a hint:

    Even in a short test script you should check if open was successful. Especially when writing to a file.

    my $output_file = 'hoser.txt'; # using three argument form and die if there's an error open my $fh, '>', $output_file or die "$output_file: open(w) failed: + $!"; my @file_info = stat $fh; or die "$output_file: stat failed: $! +"; close $fh or die "$output_file: close(w) failed +: $!";
Re: stat and localtime - take2
by viji_india (Initiate) on Feb 02, 2009 at 05:42 UTC
    Can you please tell what time do you want to print ? Because stat returns following values :- ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks)
Re: stat and localtime - take2
by lakshmananindia (Chaplain) on Feb 02, 2009 at 10:15 UTC

    I am using Perl 5.8.8. The code posted above works fine for me without any warnings. Can any one tell me why it is happening like that?

    #!/usr/bin/perl use strict; use warnings; open FILE, ">hoser.txt"; my @file_info = stat(FILE); close FILE; print join "\n", @file_info; my $timestamp = pop( @file_info ); print my $date = localtime $timestamp; __END__ 2054 1280089 33188 1 636 100 0 0 1233569457 1233569587 1233569587 4096 0Thu Jan 1 05:30:00 1970
      Because you are on a platform that does support blocks, returning a small number that causes localtime to return a date in 1970. Without support (like the OP), blocks will return as the empty string "".

      Look for stat under perlport.
Re: stat and localtime - take2
by neutron (Sexton) on Feb 02, 2009 at 01:45 UTC
    Hey thanks! Makes sense, totally missed that. I guess the next question would be ... what does it mean by blocks?
      It's the "chunks of space" allocated for that particular file. Have a look at Block_(data_storage).

      Want to find out more? Here is an excerpt from Suffering from Buffering:

        ... The blocks on your disk are probably about 8K bytes, and your computer hardware is probably designed to transfer an entire block of data from the disk at once. So instead of asking the system for a little bit of text, Perl actually asks the system for an entire blockful, which hardly takes longer to get than a little bit would have. ...
Re: stat and localtime - take2
by neutron (Sexton) on Feb 02, 2009 at 12:51 UTC
    @repellent, I haven't really looked at buffering yet. This looks like a pretty interesting article. Thanks.

    @viji_india, I wanted $ctime. I initially thought that $ctime was situated at the end of the list but then realized it is not (thanks repellent).

    @lakshmananindia, I'm not certain, but check out the perl documentation for 5.8.8. Just observing your output, it looks like the last two items in the list are different from that of 5.010

    @linuxer, I have quite a few questions about this so I think I'm going to make another post about this.