in reply to Re: -z file test operator
in thread -z file test operator

Hi, now I am using:
close (BADSYMS); if (-z BADSYMS ) { unlink "BADSYMS.out" or die ("Cant delete BADSYMS.out"); print "hi"; } else { print "Bad symbols contained in BADSYMS.out\n"; }

What happens now is when the file is zero or not zero, the if part is skipped, and it goes straight to the else. When the file is non-zero a message is returned:

Stat on unopened file <BADSYMS> at test.pl line 317

The filenames are assigned at the start of the script

unless (open (BADSYMS, ">BADSYMS.out")) { die ("Can't open output file BADSYMS.out\n"); }

This isn't in a loop as such, the script runs and the file maybe written to, if it isn't and therefore is empty, I want to delete it. If it is written to I want the print statement to output the name.

Thanx, Martin

Replies are listed 'Best First'.
Re: Re: Re: -z file test operator
by tachyon (Chancellor) on Aug 30, 2001 at 23:20 UTC

    Your problems are 1) you close the file handle then try to test it. 2) You can't unlink an file that has an open filehandle on many systems. This should be self explanatory code. $file is the FILE NAME. We open this file onto the FILE HANDLE called BAD. We can then read from the file through the filehandle.

    $file = "bad.out"; open BAD, $file or die "Oops can't open $file Perl says $!\n" # do stuff to BAD close BAD; if ( -s $file ) { # file has size } else { # file has zero size unlink $file or die "Can't unlink $file: $!\n"; }

    PS Get a login! cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print