There are two reasons that I can see right off the bat:

  1. Just a suggestion: Try using upper case file handle names rather than lower case ones.
    For example, try using 'THING' instead of 'thing'. This simply improves readability of your code.
    Using lower case is not invalid, just doesn't read as well as upper case :)

  2. What is unlink <thing>; there? Never heard of removing a file by its handle...

  3. The main reason is that you have the file open when you attempt to unlink it.
    The solution: close the file handle *before* you unlink it.

I have changed a couple of other small things (such as your $filename variable to $Form{'han'},
since that var seems useless to me, and getting rid of the print() statement.
Using the above suggestions, you get the following code:
open(THING, "$Form{'han'}.txt"); #so far, you have nothing here #code some stuff to deal with the file close(THING); unlink "$Form{'han'}.txt" or die "Couldn't delete $Form{'han'}.txt: $! +";
As you can see, there is nothing happening with the file at the moment. It just opens and closes it.

Cheers!

Update:
Ah, abstracts beat me to it and I see what he means.
You wish to read in filenames from the file and unlink those.
In that case, yes, do as abstracts says :) (or see below)

Update #2:
I couldn't let it go!
Here is another snippet that does the same thing as abstracts:

{ open NAMES, "$Form{han}.txt" or die "Oh my. File open failed: $!\n"; $/=undef; for (split("\n", <NAMES>)) { unlink or die "Couldn't delete $_: $!\n"; + } close NAMES; #Delete the file if you want to... unlink "$Form{han}.txt" or die "Couldn't delete the data file: $!\n"; }
I wrapped the whole thing in a block { } to localize the 'undef'ing of the input buffering variable ($/).
This way any files opened after this block are not affected.

Update 3:
Lol, still have one last thought on this idea.
If you only have ONE filename in the .txt file, then your code is almost right enough.
You'd just need to put the close() statement before the unlink one.
That would give you the following:

open(THING, "$Form{'han'}.txt"); unlink <THING>; close(THING); unlink "$Form{'han'}.txt" or die "Couldn't delete $Form{'han'}.txt: $! +";
That would be simple enough!
Note that this code will only work to remove a single file that is listed in the data file.
If you have any newlines in the file, it will not work.

In reply to Re: help with my unlink by mt2k
in thread help with my unlink by Jaxonn

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.