Jaxonn has asked for the wisdom of the Perl Monks concerning the following question:

Alrighty friends, i am trying to delete a .txt file but i come up short everytime. here is my code
open(thing, "$Form{'han'}.txt"); unlink <thing>; unlink "$Form{'han'}.txt" or die "Couldn't delete $filename: $!"; print "$!"; close(thing);
If you know whats wrong then i would like to know too :) Plz help me.

Replies are listed 'Best First'.
Re: help with my unlink
by abstracts (Hermit) on May 05, 2002 at 21:27 UTC
    Hello,

    If I'm understanding the problem correctly, you need to open a file named "$Form{han}.txt" which contains a list of file names you wish to delete; you want to delete these files as well we the file "$Form{han}.txt". If this is what you need, then follow the example:

    open (FNAMES, "<$Form{han}.txt") or die "Cannot open $Form{han}.txt: $!\n"; while(<FNAMES>){ # for every line in the file. chomp; # remove trailing end of line character unlink $_ or die "Cannot unlink $_: $!\n"; } close FNAMES; # close it before you unlink it. unlink "$Form{han}.txt" or die "Cannot unlink $Form{han}.txt: $!\n";
    Hope this helps,,,

    Update: If you just need to delete the file "$Form{han}.txt" then all you need to do it follow mt2k's suggestion and:

    unlink "$Form{han}.txt" or die "Cannot unlink $Form{han}.txt: $!\n";
    No need to open the file.
Re: help with my unlink
by mt2k (Hermit) on May 05, 2002 at 21:34 UTC
    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.