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

Hi everyone,

I'm trying to implement a pretty simple code using 'stat' and 'localtime' as follows:
#!perl use warnings; use strict; use 5.010; my @file_info = stat($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 71.
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.

Original content restored (above) by GrandFather

I screwed up the initial post. Not sure how to delete it but I will repost later.

Replies are listed 'Best First'.
Re: Trying to understand the warning.
by Corion (Patriarch) on Feb 01, 2009 at 21:35 UTC

    Your code as typed does not work, because $file is never initialized. And stat says

    Returns a null list if the stat fails.

    So you should first check that the file named in $file exists:

    if (! -e $file) { die "Uhoh - '$file' does not exist."; };

    and also, you should look at what you get in @file_info and $timestamp:

    use Data::Dumper; print Dumper \@file_info;

    Maybe you are running afoul of the meaning of backslashes in Perl and backslashes in Windows filenames? When specifying Windows filenames in Perl scripts, you need to double up every backslash:

    my $file = "C:\\boot.ini"; print $file;
      So you should first check that the file named in $file exists:

      "Check first" is a bad meme, because it might suffer from race conditions.

      That's why I'd stat once, and then check if the return value is a null list.

Re: Trying to understand the warning.
by merlyn (Sage) on Feb 01, 2009 at 21:32 UTC
    Why are you trying to turn "number of blocks in the file" into a timestamp? But even if you fixed that, are you sure you're not getting undef in $timestamp, because the stat failed? After all, this code can't even compile, so you aren't showing the actual code. How about showing the actual code?
Re: Trying to understand the warning.
by moritz (Cardinal) on Feb 01, 2009 at 22:01 UTC
    It would be nice if you gave us an example that actually reproduced the warning you have shown us.

    There are no 71 lines in your example code, and when I it with a non-existing file I get Use of uninitialized value $timestamp in localtime at foo.pl line 12., and no warning with an existing file.

Re: Trying to understand the warning.
by neutron (Sexton) on Feb 01, 2009 at 21:49 UTC
    My bad, I forgot to include the file definition (it's just some random file, nothing actually in it). I've included the definition now, so please, critique away ...

Re: Trying to understand the warning.
by neutron (Sexton) on Feb 01, 2009 at 22:00 UTC
    Oh, and I guess you'll probably need a file named "hoser.txt".