Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: -e and unlink

by japhy (Canon)
on Mar 14, 2001 at 03:47 UTC ( [id://64267]=note: print w/replies, xml ) Need Help??


in reply to -e and unlink

The condition of that if statement is pointless. You can't delete a file that doesn't exist.
unlink $filename;
is fine.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: -e and unlink
by dws (Chancellor) on Mar 14, 2001 at 04:34 UTC
    japhy's reply may be worth expanding on.

    IF you don't care about reporting errors, then     unlink $filename; is sufficient.

    IF you DO care about reporting errors (such as a failure to remove a file that your script lacks permission to remove), then you'll want to do something like:

    if ( -e $filename ) { unlink($filename) or die "$filename: $!" }
      But you can still avoid the -e test:
      unlink $filename or $! == 2 or die "Cannot unlink $filename: $!";
      On my system, "2" is the code for a missing file for unlink. If you don't mind the regex hit, you could also use:
      unlink $filename or $! =~ /no such/i or die "Cannot unlink: $filename: + $!";

      -- Randal L. Schwartz, Perl hacker


      update:

      This is actually faster than a separate test and unlink (one O/S call instead of two), and is less prone to race conditions. I'd actually scream at someone who did a -e/unlink pair instead of this in a security or performance related application.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://64267]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found