Re: What is the best way to delete a file in windows using Perl?
by m.att (Pilgrim) on Apr 25, 2006 at 16:52 UTC
|
Check out unlink:perldoc -f unlink | [reply] [d/l] |
|
|
sub Check_request_file {
my ($file_to_check) = @_;
local *_; # Protect caller's $_.
open(local *FILE, '<', $file_to_check)
or die("Unable to open SPROC file $file_to_check: $!\n");
while (<FILE>) {
if (/SPROC\sName\:(?!\s[rR]eception)/) {
close(FILE); # So we can delete it
unlink($file_to_check)
or die("Unable to delete SPROC file $file_to_check: $!\n");
return; # Optional, since <FILE> will
# return false for a closed file.
}
}
}
- You had two names ($flat_to_check and $file_to_check) for the same var.
- I switched rename to unlink.
- I changed exit to return.
- I fixed the regexp ([r|R] vs [rR]). See perlre.
- I closed the file before attempting to delete it. (Not necessary on all OSs, but required on some.)
- I used the safer 3 argument open.
- I limited the scope of FILE.
- I added error checking to the open.
- I improved the error message for deletion errors.
- I simplified the condition.
- I removed some useless quotes ("$file_to_check" vs $file_to_check).
- I removed the useless empty else clause.
- Update: I protected the caller's $_. while (my $line = <FILE>) would also have worked, if the regexp is matched against $line.
| [reply] [d/l] [select] |
Re: What is the best way to delete a file in windows using Perl?
by swampyankee (Parson) on Apr 25, 2006 at 17:14 UTC
|
Well, you would have to define "work OK" for anybody to answer that question. In this case, I think the answer is "No," because $flat_to_check is not defined, remame is not a function, and if it's a typo for rename, I don't believe you can set a file name to a zero-length string on any O/S. Also, you probably won't be able rename or delete it while it's open.
So:
- Define $flat_to_check.
- You should always check to see if you could open a file. Incidently, the < is superfluous.
- You probably want unlink vs rename.
- You probably can't delete or rename a file while it's open, so you'll have to explicitly close it before trying to delete it. If I remember, Perl does not automatically close files unless an open is executed with the same filehandle.
grammar correction
emc
"Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. " —G. Steele
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: What is the best way to delete a file in windows using Perl?
by eric256 (Parson) on Apr 25, 2006 at 17:39 UTC
|
If its awkard to test then need to figure out a better way to test it. We all have aspects of programs that might be sensitive (i.e. deleting files) but you still want to figure out how to test it and make sure that it always works right. Its painfully obvious that the code you posted wont even run, let alone delete files, which makes it hard for anyone to want to help you if you can't help yourself.
| [reply] |
Re: What is the best way to delete a file in windows using Perl?
by davorg (Chancellor) on Apr 26, 2006 at 08:25 UTC
|
It is a bit awkward for me to test this bit of code
Perl Testing: A Developer's Notebook has plenty of advice on testing stuff that seems to be difficult to test. It's well worth getting a copy.
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
Re: What is the best way to delete a file in windows using Perl?
by blazar (Canon) on Apr 26, 2006 at 09:01 UTC
|
remame "$flat_to_check", "" or warn $!;
remame? No, I think it wouldn't "work ok" even if it were supposed to. Hint: do not retype code, paste it. And if you did paste it, at least check the syntax... | [reply] [d/l] |
Re: What is the best way to delete a file in windows using Perl?
by Asim (Hermit) on Apr 26, 2006 at 12:50 UTC
|
To help with testing, I recommend what I oftentimes do to test my code. Copy the actual file you load into your code as $file_to_check to a new subdirectory. In there, run a copy of your script against copies of the copy and/or making a mock version of the file -- bang! Instant Clean testing!
You can even trim the code copy down to just the bits that you're worried about, esp. if the code does other things, as well. That's something that reference to Perl Testing: A Developer's Notebook will help school you in, so I second davorg's wise recommendation in that area.
Also, in your loop, I would personally recommend, on top of ikegami's rewrite, that you drop out of the while loop with a last before you delete. You do need to close the file first, and having that close be outside the loop will make it easier for others to read now, and likely avoid some issues in the future.
----Asim, known to some as Woodrow.
| [reply] [d/l] [select] |
Re: What is the best way to delete a file in windows using Perl?
by gellyfish (Monsignor) on Apr 26, 2006 at 08:43 UTC
|
S:\>echo >tr2
S:\>perl -e "rename 'tr2','' or warn $!"
No such file or directory at -e line 1.
S:\>
It wasn't really all that awkward to test that part now was it?
/J\ | [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |