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

Mighty monk, how can i delete a file with unlink and the file name is pass in from another .pl file to call the delete file package.

#the pass in is just the name of the file without the extension $t=".tm" #this is use for the file extension, sub DeleteFile() { my $FileName=$_[0]; $FileName="$FileName$t"; unlink '$FileName$t'; }
it didn't delete the file athough it outcome no error. thank you for the help.

Edit 2001-06-19 ar0n -- Formatting fix

Replies are listed 'Best First'.
Re: How come i cannot delete file with unlink
by azatoth (Curate) on Jun 19, 2001 at 10:39 UTC
Re: How come I cannot delete file with unlink
by Brovnik (Hermit) on Jun 19, 2001 at 12:43 UTC
    Your main problem is that the '' in the unlink line don't evaluate the variable.

    You need unlink $FileName in the last line.

    It will tell you how many files it deleted, and you could report this.

    sub DeleteFile() { my $FileName = $_[0] . $t; $ret = unlink $FileName; die "No file called [$Filename] deleted\n" unless $ret == 1; }
    or, more concisely
    sub DeleteFile() { my $FileName = $_[0] . $t; die "No file called [$Filename] deleted\n" unless unlink $FileName; }

    Update: added third option.

    or, you can be more friendly and return the value to the caller :

    sub DeleteFile() { my $FileName = $_[0] . $t; my $ret = unlink $FileName; warn "No file called [$Filename] deleted\n" unless $ret; return $ret; }
    Which of the last 2 to choose depends on how important it is that the file is deleted or whether you have already checked its existence.
    --
    Brovnik
Re: How come i cannot delete file with unlink
by Beatnik (Parson) on Jun 19, 2001 at 11:08 UTC
    Let's say you want to unlink foobar.tm and you pass foobar to you DeleteFile routine. Inside that routine, $FileName first gets foobar, then .tm is added, then foobar.tm.tm is unlinked.

    What I'm trying to say is, Unless your file has 2 extensions (both being .tm), you should drop that $t in the unlink line.

    Oh yeah, listen to what azatoth has to say about strict and -w.

    Update courtesy of Arguile: Use double quotes (as in ") instead of single quotes (as in '). The double quotes interpolate properly, the single ones use literal context.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: How come I cannot delete file with unlink
by Xxaxx (Monk) on Jun 19, 2001 at 22:08 UTC
    Your code:
    $t=".tm" #this is use for the file extension, sub DeleteFile() { my $FileName=$_[0]; $FileName="$FileName$t"; unlink '$FileName$t'; }
    The line $FileName="$FileName$t"; will properly create the file name with extention.

    The " is very different from '. In quoting with " the scalars inside are substituted with their values.

    Your next line is scrambled. First of all you are using single quote ' which will not interpret variables. Second of all you are already added the extention onto $FileName. So you don't need to do it again.

    If you just change the last line to:

    unlink $FileName;
    Then the subroutine will work. However, please read over the answers given above. Especially the part about testing and reacting to the return value from the unlink. You should always collect, test, and respond to return values. Otherwise you are left with occasional mysteries and maybe some very messed up results.

    As you can see by the many different suggestions above: "There are many ways to skin a cat in Perl". (Pun intended) ;-)

    Do yourself a favor and always include the following when presenting a code snippet:

    #!/usr/bin/perl -w use strict;
    This demonstrates that you have submitted your code to these two excellent forms of error checking. This has many useful effects. 1) You catch the embarassingly simply typo errors and don't waste your time typing in a question and give the monks the respect of "doing your homework". 2) The use of -w and strict will gently force you to program in ways which can help prevent many of the more avoidable errors. 3) By including the -w and use strict in your code snippet it communicates effectively that you've already handled that part.

    Also it is an excellent habit to lookup keywords related to your question. At the top of almost every page at perlmonks.com there is a search box. If you type a key word such as "unlink" into this box and do a search you will be presented with a man page for that keyword. Not only will the man page give you the background info on the function it will also include several pieces of example code. Sometimes these will be exactly what you need and sometimes you will need to modify the examples a bit.

    For example: perlfunc:unlink
    Deletes a list of files. Returns the number of files successfully deleted.

    $cnt = unlink 'a', 'b', 'c';
    unlink @goners;
    unlink <*.bak>;

    None of the examples are exactly what you are looking for. The bottom example looks new and unusual. You can mark that for later study. The top example looks really close to your needs. In fact if you just delete the 'b', 'c' from the line you have something almost exactly what you are looking for. It is not much of a leap to figure out $cnt = unlink $FileName;

    Hope this opens up a few windows on how to squeeze the best from perlmonks and upscale your code?

    Claude

Re: How come I cannot delete file with unlink
by Abigail (Deacon) on Jun 20, 2001 at 01:12 UTC
    You are making several serious mistakes here.

    First mistake is that you are not checking the return value of unlink. I'm a bit surprised you made it to the front page with such a mistake. There's no need to go asking around why your program "doesn't work" if you refuse to let Perl help you. Always, ALWAYS, ALWAYS check the return value of system calls. Just do it. $! would have told you what was wrong. You say it outcome no error. Well, duh. You're ignoring the error, silencing Perl.

    Your second mistake is the use of single quotes, as was pointed out in another post. You need double quotes there, or the concatenation operator.

    Third mistake is assuming that unlinking a file actually deletes it. It doesn't have to. A file could still have other names (hard links), or open file handles. While the name might be gone from the filesystem, the file itself only disappears if all its names are gone, and all the open handles are closed.

    -- Abigail