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:

use 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"); }
Running this program results in the output
2 lines read *************** Could not delete \\mucsdn21\fischron\tmp\aftermath_res +ult.txt (Permission denied)
and the file still exists afterwards. If I comment out the binmode statement and run the program, the file is deleted properly.

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.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Closing a filehandle in ":unix" layer doesn't work on Win32 by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.