Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How come I cannot delete file with unlink

by Xxaxx (Monk)
on Jun 19, 2001 at 22:08 UTC ( [id://89759]=note: print w/replies, xml ) Need Help??


in reply to How come I cannot delete file with unlink

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found