Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: help with my unlink

by mt2k (Hermit)
on May 05, 2002 at 21:34 UTC ( [id://164175]=note: print w/replies, xml ) Need Help??


in reply to help with my unlink

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://164175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found