in reply to Whoops! I deleted everything!

What am I missing?

A bunch of data apparently... (I couldn't resist.)

I know you don't want to hear this now, but you should never run (even as a test) code like this without neutering it first. Don't unlink anything but have it write the names of the files it would have unlinked to a file for inspection. Only after you are sure that it won't do anything harmful, run it on a test directory. Triple check it before you run it in a production environment.

The problem is with your $days variable. You declared it with my() twice. Apparently, that results in some freakish behavior. During the first call to File::Find::find, $days is undefined. It is properly set to 7 during the second call.

Using warnings would have helped you spot this bug early.

By the way, if ($#deletions == 0 ) is not what you mean. That will be true if @deletions has a single element as the index of that element will indeed be zero. You mean if (@deletions == 0) which will be true if @deletions has no elements.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Whoops! I deleted everything!
by Anonymous Monk on Dec 11, 2002 at 02:15 UTC
    Thanks for your insight. I was too confident about my code, so I let it run. Big mistake. I will always use warnings and do more testing in the future.

    The problem is with your $days variable. You declared it with my() twice. Apparently, that results in some freakish behavior.

    Is it really a bug? It just seems like it should work. Would it have worked properly if I hadn't used my() and just made $days global? What would be the best solution?

      Well, when you declare it twice, the first declaration overides the second and it is like the first one didn't exist at all. You could comment out the my $days = 31; and your program will behave exactly the same.

      It would have worked if $days was global but I don't think that's the best solution. I think the best solution is simply to remove the 'my' on the line where you set my $days = 7;. So just change that line to $days = 7; and that should do the trick.

      Of course, I still recommend testing thoroughly before actually allowing it to unlink files. ;-)

      -sauoq
      "My two cents aren't worth a dime.";