in reply to Re: Deleting Multiple Files
in thread Deleting Multiple Files

unlink join ",", @files_to_delete;

I don't think that is going to do what you expect it to do. If you have three files named a, b and c, it is literally going to try to delete a file named a,b,c. What you need is simply:

my $nr = unlink @files_to_delete; if( $nr != scalar @files_to_delete ) { print "uh-oh: helpful error message here"; }

I would, however, make the script run with taint checks enabled, and carefully untaint the filenames before passing them to unlink. I would also deal with each file one by one anyway:

for( @files_to_delete ) { # untaint $_ here unlink or print "failed to remove $_: $!\n"; }

update: looking at the OP more carefully, there are two other points to be made: firstly, you don't need to open a file in order to remove it. If you want to test that it's there you can use the -f operator. But I would just try and delete it and see what happens. Some things you can't test for, you just have to try it and see whether it works.

Secondly, unlink works in terms of the process' current directory. This might not be where you think it is. To remove the possibility of unexpected surprises, you should really be doing something like unlink "$path_to/$_" in my snippet above, where $path_to contains the name of the the directory /all/the/way/from/the/top.

_____________________________________________
Come to YAPC::Europe 2003 in Paris, 23-25 July 2003.

Replies are listed 'Best First'.
Re: Re:x2 Deleting Multiple Files
by shemp (Deacon) on Jun 04, 2003 at 21:57 UTC
    Ah shoot, you're correct, it should just be
    unlink @files_to_delete;
    brainfart
    shemp
Re: Re:x2 Deleting Multiple Files
by lisaw (Beadle) on Jun 05, 2003 at 01:41 UTC
    I seem to be running into a wall converting this over to use CGI. I keep getting an error 500
      We can help you with it if you post it on your scratchpad.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.