Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Correct code for deleting old file?

by rovf (Priest)
on Jun 15, 2012 at 10:57 UTC ( [id://976412]=note: print w/replies, xml ) Need Help??


in reply to Correct code for deleting old file?

If you don't check the result of unlink (i.e. whether it works or not), you can equally well omit the test for existence. Hence, I would write it either as
unlink(@taxonomy_file_list);
or as
(unlink($_)==1 || report_unlink_error($_,$!)) for @taxonomy_file_list;

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^2: Correct code for deleting old file?
by thirdm (Sexton) on Jun 15, 2012 at 20:04 UTC
    Kind of depends on what you want to report as an error though. I'd think you'd want to be quiet if the file doesn't exist yet but whine if it exists and unlink fails (maybe the file or its directory got to be read only for you somehow).
Re^2: Correct code for deleting old file?
by cavac (Parson) on Jun 17, 2012 at 21:35 UTC

    I'm not sure about this. My information may be a bit out of date.

    Calling unlink on some non-file types (directory or similar) still has "unspecified" consequences on some operating systems, hasn't it? And -e only checks for existence, whereas -f would check specifically for a file, making the call to unlink safer, right?

    "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."
      Basically i agree with you. I had assumed that you from your knowledge of your application, you know for sure that the names which you had collected in your file list, certainly contain plain files (and not directories, symbolic links or what else). In this case (only) it makes sense to omit the file testing.

      If this is not the case, I personally would be interested *which* non-file entries made it into the file list, because this (possibly) signals an error. In this case, I would do a -f test for each file, but I would also issue an error message for those entries which are not plain files.

      -- 
      Ronald Fischer <ynnor@mm.st>

        Thanks for the clarification.

        I'm not the OP. I was just curious. I've been avoiding unlink for non-files for something like a decade now. For a nanosecond there i thought maybe someone actually managed to to fix unlink in libc ;-)

        "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found