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.


In reply to Re:x2 Deleting Multiple Files by grinder
in thread Deleting Multiple Files by lisaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.