rovf has asked for the wisdom of the Perl Monks concerning the following question:
A text file is created on a Unix system. Some time later, my Perl program accesses this text file. The Perl program runs on a Windows system, the access to the Unix share is done using CIFS, and the file is specified using UNC path syntax. "Accessing the file" in this case means that the file is opened, read, closed, and deleted. Deletion does not work if the file was read using ':unix' layer. The problem is reproducible with Perl 5.8.8 and 5.10.0. Here is the example program:
Running this program results in the outputuse strict; use warnings; use IO::File; my $filepath='\\\\mucsdn21\\fischron\\tmp\\created_on_unix.txt'; if(-f $filepath) { my $fh=IO::File->new(); $fh->open($filepath) || die "$!"; binmode($fh,':unix'); my @result=<$fh>; $fh->close || die "$!"; print(@result." lines read\n"); $!=0; # Documentation does not say whether or not unlink sets $! o +n error if(!unlink($filepath)) { print("*************** Could not delete $filepath ($!)\n"); } } else { print("$filepath does not exist"); }
and the file still exists afterwards. If I comment out the binmode statement and run the program, the file is deleted properly.2 lines read *************** Could not delete \\mucsdn21\fischron\tmp\aftermath_res +ult.txt (Permission denied)
Further analysis with a slightly modified test program, and using Process Explorer, reveals that at the time the unlink function is executed, the process still has an open handle to the file to be deleted. I conclude that the close statement doesn't properly close the file.
The reason why I use binmode(...,':unix') is that the file was created on Unix using Unix line separators, but I want to read it on Windows. I found that this is not strictly necessary: I can read the file equally well using the default layer. Still, I wonder why my original solution did not work.
|
|---|