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

Beloved Gods

I have the Following script
use strict; use Win32; use Win32::ODBC; use File::Find; use File::stat; use File::Spec; use Time::localtime; system("cls"); my $start_time = ctime(); print "\nStart Time: $start_time\n\n"; print "Full Path,Size (Byte),Created,Modified,Accessed,File Name,Type\ +n"; find(\&wanted, @ARGV); sub wanted { return unless -f; my $sb = stat ($File::Find::name); my $ct = localtime $sb->ctime; my $mt = localtime $sb->mtime; my $at = localtime $sb->atime; my $size = $sb->size; my ($volume,$directories,$file) = File::Spec->splitpath($File::Fin +d::name); my $ext = $file; $ext =~ s/\.(.+)$//; printf "%s,%s,%02d/%02d/%02d,%02d/%02d/%02d,%02d/%02d/%02d,%s,%s\n +", $File::Find::name, $sb->size, $ct->mon()+1,$ct->mday(),$ct->year %100, $mt->mon()+1,$mt->mday(),$mt->year %100, $at->mon()+1,$at->mday(),$at->year %100, $file,$1 } my $end_time = ctime(); print "\nEnd Time: $end_time\n";
That produced this error
C:\Perl\test>file_stat.pl "t:/international" > c:\res\inter.csv The handle is invalid. Can't call method "mon" on an undefined value at C:\Perl\test\file_sta +t.pl lin e 34. C:\Perl\test>
Any idea why this is? and how can I get the script to contineu to run even if it encounters error such as that?

Couldn't thank you enough.

Blackadder

Replies are listed 'Best First'.
Re: Invalid File Handle???
by Corion (Patriarch) on Jul 13, 2005 at 11:12 UTC

    You should check if the file system supports atime at all before trying to use it. My guess is that on your system, $sb->atime did not return something usable that localtime could use. In fact, I'm wondering a bit that your localtime seems to return objects, but that may or may not be.

    In the end, maybe some code $at ||= localtime or some other sensible default for $at should make your script work in the cases where atime does not return a sensible value.

      Thanks,...How can I check my windows 2000 desktop to see if it can support atime?

      Also, Not sure on how to use this notation $at ||=localtime! can I just use it in my code as
      my $at ||= localtime $sb->atime;
      Is the above correct?

      or this is correct?
      my $at = localtime $sb->atime || 'UNKNOWN';
      Thanks for the enlightenment.
      Blackadder

        The problem originates from the fact that seemingly, the following code does not return an object:

        my $at = localtime $sb->atime;

        You could print out the result of $sb->atime, or look into the documentation of the module to find out how it behaves on your Windows 2000 desktop.

        As you are using $at as an object later on, you want to synthetize an object for the "unknown" value as well. As I don't know what kind of module you are using that overrides localtime, I can't offer much more help here.

        Probably not much help, but a confirmation, this script seems to work fine on my Win2K machine. No errors, reasonable results... Tested on Win2K, NTFS and Fat32 file systems, ActivePerl 5.8.6 build 811.

Re: Invalid File Handle???
by NetWallah (Canon) on Jul 13, 2005 at 16:55 UTC
    Change the stat call line to :
    my $sb = stat($_) or die "ERROR:Could not stat $_\n$!";
    I did some debugging, and found a difference in the behaviour of stat, when called with $_ as opposed to the full path passed by $File::Find::name. The file name ($_) works better than the full path, since File::Find already puts you in the correct directory context.

         "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk