in reply to What is the best way to delete a file in windows using Perl?

Check out unlink:

perldoc -f unlink

Replies are listed 'Best First'.
Re^2: What is the best way to delete a file in windows using Perl?
by ikegami (Patriarch) on Apr 25, 2006 at 16:54 UTC

    or on the web: unlink

    Update: I cleaned up and fixed the OP's code:

    sub Check_request_file { my ($file_to_check) = @_; local *_; # Protect caller's $_. open(local *FILE, '<', $file_to_check) or die("Unable to open SPROC file $file_to_check: $!\n"); while (<FILE>) { if (/SPROC\sName\:(?!\s[rR]eception)/) { close(FILE); # So we can delete it unlink($file_to_check) or die("Unable to delete SPROC file $file_to_check: $!\n"); return; # Optional, since <FILE> will # return false for a closed file. } } }
    • You had two names ($flat_to_check and $file_to_check) for the same var.
    • I switched rename to unlink.
    • I changed exit to return.
    • I fixed the regexp ([r|R] vs [rR]). See perlre.
    • I closed the file before attempting to delete it. (Not necessary on all OSs, but required on some.)
    • I used the safer 3 argument open.
    • I limited the scope of FILE.
    • I added error checking to the open.
    • I improved the error message for deletion errors.
    • I simplified the condition.
    • I removed some useless quotes ("$file_to_check" vs $file_to_check).
    • I removed the useless empty else clause.
    • Update: I protected the caller's $_. while (my $line = <FILE>) would also have worked, if the regexp is matched against $line.