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


In reply to Re: How come I cannot delete file with unlink by Xxaxx
in thread How come I cannot delete file with unlink by Anonymous Monk

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.