I have an application running on Windows 7 with Perl 5.14, where a file is opened for reading at one place, but the layer is set at a different place using binmode. This has the weird effect that the file can not be deleted, after it is closed, while the process owning the handle is still running. My feeling it that close doesn't work properly, although it doesn't return an error.

Here is a small, complete program which demonstrates the problem:

use strict; use warnings; # Some file is created by a different process my $file='ttz'; system("cmd /c echo xx >$file"); # In our application, this file is created by a process # on Unix. That's why we read it with the Unix layer. # We do NOT set the layer during the open call. my $layer=':unix'; open(my $fh,$file) or die "open error $!"; # In our application, a different function is responsible # for setting the layer. Therefore, we use binmode to set # it. Nothing had been read from the file so far, so this # should be fine. binmode($fh,$layer); # Now close the file ... close($fh) or die "close error $!"; # ... and try to delete it. if(!unlink($file)) { # this is line 19 warn $!; system("cmd /c del $file"); }
Running this program on my PC shows the following output:

Permission denied at fcstest1.pl line 19. I:\tmp\ttx The process cannot access the file because it is being used by another + process.
-- 
Ronald Fischer <ynnor@mm.st>

In reply to Win32: Setting a layer with binmode causes problem with close() on Windows 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.