Renaming File

I am currently using the script below to delete a line from a file. The script is running on an NT system and works fine up until the part where the tmp file is renamed to the live file. If there a different way of doing this or how do I make sure that the renaming goes through?

sub delt{
$file = "$keyfile";
open (FILE,"$file");
 open (TMPFL,">>key.tmp");
@keys = <FILE>;
 $q = 0;
  foreach $line (@keys){
     if ($key ne $line) {print TMPFL "$line\n";}
  rename "key.tmp", $file || die("Unable to rename.");
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MAXcomAuthor Commented:
doing "rm $file;" seems to have no effect and doesnt delete the file.
Kim RyanIT ConsultantCommented:
Your code looks OK as you test for the return status of the rename function (1 for success). Are you sure $file contains a valid file name? WHat errors are you getting?

You can use this to achive the same thing.
use File::Copy;

unlink($file); is the way to delete a file. But if you rename it, there is no need to delete it.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MAXcomAuthor Commented:
great! unlink did it
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Your function may do interesting things if more than one processes try to run it at the same time.
$file seems to be unnecessarily interpolated twice.
For a large file, it may be more memory efficient to process one line at a time than to read the entire file into memory.
the last line in TMPFL will end in \n even if the last line in FILE did not.
You aren't checking the success of your opens
Altering global variables within a sub may be unexpected to the caller
rename "key.tmp", $file || die("Unable to rename.");
parses as
 rename "key.tmp", ($file || die("Unable to rename."));
MAXcomAuthor Commented:
ozo, how many points do you want to optimize that function since its crucial to what im doing?
I'm not sure what you want to optimize for, if speed, it probably runs about as fast as disk reads/writes now.
Optimizing for code simplicity, I might have written something like

sub delt{
   local $^I=".tmp"; local @ARGV=($file);
   $key .= $/;
   while( <> ){
      print unless $_ eq $key;
   chomp $key;

(except that I would have passed parameters instead of globals)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.