http://qs321.pair.com?node_id=64256

DeusVult has asked for the wisdom of the Perl Monks concerning the following question:

I have been cursed with the task of adapting...<shudder>...legacy code. I have many problems, but I'll only ask for your kindly help on one at a time.

Inescapable Rule of Programming #1: before one can hope to debug code, one must understand what it does.

Problem the first

Context: there is a database, and this script reads a text file which describes the sorts of changes that need to be made to the DB (done thankfully, through a separate API that I need not concern myself with).

Every now and again in the program, there appears a line like this: if (-e $filename) { unlink ($filename); }

Try as I might, I can find neither rhyme nor reason to these lines. Unfortunately, I am additionally hampered by a lack of perl reference manuals and a broken perldoc! So all I have at my disposal is my memory and, of course, perlmonks :)

What confuses me is that I thought -e checked for existence. So that line of code reads "if the file exists, unlink it."

What really confuses me is the call to unlink. I'm not entirely sure what it does, but it sounds sinister. Why this is so confusing is that the files that are being so wantonly unlinked are fairly important data files necessary for the execution of the script (they describe the necessary DB changes), so it seems to me odd that the script would be doing something untoward to them. From a little PM supersearching, I found that unlink seems to have something to do with symbolic links, but that makes no sense in this context because the files in question are not symbolically linked to anything!

So my fellow monks, my questions for you are these:

  1. Is my understanding of -e correct?
  2. What exactly does unlink do, and is it as viscious toward the target file as it sounds?
  3. Taken as a whole, what does that if statement do?
  4. From a philosophical standpoint, can anyone think of a reason why someone might want to do whatever that is?

As always, my thanks go out to the selfless denizens of perlmonks.

Some people drink from the fountain of knowledge, others just gargle.