in reply to -z file test operator

You are testing whether a file called BADSYMS is zero sized (if (-z BADSYMS)), but trying to remove a file called BADSYMS.out. You're not quoting the filename in this line either.

Also, there's no need to fork off a shell for rm. Use Perl's builtin unlink instead.

Try this:

use strict; use warnings; if (-z 'BADSYMS.out') { unlink 'BADSYMS.out' or die 'Could not delete BADSYMS.out'; } else { print "Bad symbols contained in BADSYMS.out\n"; }

Replies are listed 'Best First'.
Re (tilly) 2: -z file test operator
by tilly (Archbishop) on Aug 30, 2001 at 17:04 UTC
    2 important notes on the error check.

    First of all you should make sure that your die message contains the output of $!, the operating system error will often greatly simplify debugging.

    Secondly I suggest putting the name of the file into a variable and using that variable in both the open and the die. The reason is that otherwise it is far too easy for someone to come along and change what file is being opened but not edit the error check. That can be a real pain to debug. When you can, you want to reduce the need to synchronize code...

Re: Re: -z file test operator
by Anonymous Monk on Aug 30, 2001 at 17:08 UTC
    I opened the file using

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

    So i presumed that when using the -z I could test against the filehandle, do I need to use the quotes?

Re: Re: -z file test operator
by Anonymous Monk on Aug 30, 2001 at 17:32 UTC
    Now I get this error message

    Can't exec "BADSYMS.out": Permission denied

    This is the code I now have in:

    if (-z `BADSYMS.out` ) { # unlink `BADSYMS.out` or die `Cant delete BADSYMS.out`; print "hi"; } else { print "Bad symbols contained in BADSYMS.out\n"; }

    I have commented out the unlink, as it was going straight to it, regardless of whether the file was 0 or not. When it prints out the "hi" regardless I know the -z isn't working

    any ideas?

      Don't use backticks like `BADSYMS.out` - instead use normal single or double quotes:

      if (-z "BADSYMS.out") { # do something }
      The backticks try to execute the file which is not what you want.

      -- Hofmator