"Peter Juuls" <> wrote:
> In my script I fetch the filesize with stat($_)->size from File::stat.
> Occasionally it fails although the file exists, and perl exits with this
> error on STDERR: Can't call method "size" on an undefined value at
> myscript.pl line 7.
If you change it to:
(stat($_) or die $!)->size;
Then at least you will get the real error message.
>
> I don't want the script to exit, instead I want to ignore the "bad" file
> and continue with the next file. I have tried to do tests on
> file-existence BEFORE the stat->size is executed,
You probably have a race condition, where the file exists the first 3 times
you stat it, but not the final time.
> to prevent perl from
> exiting, but nothing seems to work.
If you don't want Perl to exit, don't call the size method on an undefined
value. The easiest way to do that is to check the definedness of the value
before calling the method.
>
> use File::stat
> .....
> if ((-e $_) && (-r $_) && ($st = stat($_)) {
> $bytes = stat($_)->size ;
> }
my $st=stat($_);
if (defined $st) {
$bytes=$st->size;
};
Xho
--
--------------------
http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB