Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found