unable to delete a file

C

cartercc

The script downloads a file named EAUSR.txt from a remote server. The
contents of the file are new each day, that is, I always have to
download a new EAUSR.txt in order to process the new data. After I
process the data, I need to delete EAUSR.txt to prepare for the next
day's download.

Here is the code I use:

1 $file = get_daily_file(); # gets EAUSR.txt
2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
3 process_daily_file(); # calls a number of different subs
4 close INFILE;
5 unlink $file; # does not delete EAUSR.txt
6 exit();

Here's the problem: After the script exits, EAUSR.txt stil exists. If
I move line 5 to the beginning of the script, it works fine --
EAUSR.txt is deleted before I get each day's new file. The funny thing
is that my script has run for years deleting the file at the end, as
shown above, but a couple of days ago, I made some extensive revisions
to the script and now it doesn't work.

It's not a permissions problem. It's not because INFILE is still open
(I don't think). It's not because unlink doesn't work. I don't have
any idea why unlink doesn't work at the end, but works at the
beginning. Any ideas?

Question: is there any version of close that closes all open
filehandles? The script as a whole is about 500 lines long, and in
running opens and closes dozens of files, and I'm thinking that maybe
the reason the file isn't deleted is because I have inadvertently not
closed it, although I have paired up all calls to open with a call to
close.

TIA, CC.
 
T

Tad J McClellan

cartercc said:
5 unlink $file; # does not delete EAUSR.txt


Ask it why it failed by checking the return value and including $!

unlink $file or die "could not delete '$file' $!";
 
G

Gunnar Hjalmarsson

cartercc said:
1 $file = get_daily_file(); # gets EAUSR.txt
2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
3 process_daily_file(); # calls a number of different subs
4 close INFILE;
5 unlink $file; # does not delete EAUSR.txt
6 exit();

Here's the problem: After the script exits, EAUSR.txt stil exists.
....

... a couple of days ago, I made some extensive revisions
to the script and now it doesn't work.

It's not a permissions problem. It's not because INFILE is still open
(I don't think).

How about letting Perl help you check the latter?

close INFILE or die $!;
It's not because unlink doesn't work.

How about ensuring that that's the case?

unlink $file or die $!;
Question: is there any version of close that closes all open
filehandles? The script as a whole is about 500 lines long, and in
running opens and closes dozens of files, and I'm thinking that maybe
the reason the file isn't deleted is because I have inadvertently not
closed it,

If that's the case, the error message resulting from the failed
unlink()ing ought to tell you.
 
T

Tim Greer

cartercc said:
5 unlink $file;                  # does not delete EAUSR.txt

It could be anything from the mount options on the drive, to file
attribute settings, to anything else. Check the return value and the
error if it fails, like you would on an open call unlink $file or die
"can't unlike $file $!";
 
C

cartercc

Ask it why it failed by checking the return value and including $!

   unlink $file or die "could not delete '$file' $!";

Funny. I just tried this, and unlink deleted the EAUSR file just like
it was supposed to. Did not get any error message at all.

Must be the McClellan hex -- die or else!

CC
 
T

Tad J McClellan

cartercc said:
Funny. I just tried this, and unlink deleted the EAUSR file just like
it was supposed to. Did not get any error message at all.

Must be the McClellan hex -- die or else!


You have the order of the operands reversed. It's more like:

Do what I ask or die!

:)
 
C

cartercc

You have the order of the operands reversed. It's more like:

   Do what I ask or die!

:)

Yeah, but it's funnier the way I wrote it, as if dying is the best
option, not the worst. What other options do you have if the first one
is die?

Your way is the same as 'your money or your life' which is more of a
dog-bites-man story rather than a man-bites-dog story.
 
T

Tim Greer

cartercc said:
Yeah, but it's funnier the way I wrote it, as if dying is the best
option, not the worst. What other options do you have if the first one
is die?

So, don't use die, use something else relevant and helpful to report
failures (or log them), or whatever it is you want to do in said
situation.
 
E

Eric Pozharski

On 2009-02-05 said:
1 $file = get_daily_file(); # gets EAUSR.txt
2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
3 process_daily_file(); # calls a number of different subs
4 close INFILE;
5 unlink $file; # does not delete EAUSR.txt
6 exit(); *SKIP*
It's not a permissions problem. It's not because INFILE is still open
(I don't think). It's not because unlink doesn't work. I don't have
any idea why unlink doesn't work at the end, but works at the
beginning. Any ideas?

perl -wle '
my $fn = q|foo|;
print q|one: |, -e $fn ? 1 : 0;
open my $fh, q|>|, $fn or die $!;
print q|two: |, -e $fn ? 1 : 0;
unlink $fn or die $!;
print q|three: |, -e $fn ? 1 : 0;
close $fh;
print q|four: |, -e $fn ? 1 : 0;
'
one: 0
two: 1
three: 0
four: 0

So the question is: what's your platform?

*CUT*
 
J

Josef Moellers

cartercc said:
Funny. I just tried this, and unlink deleted the EAUSR file just like
it was supposed to. Did not get any error message at all.

It looks as if you had a bug in the original program which was fixed
when you added the "or die ..." part.

Not unusual.

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top