Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Win32: Setting a layer with binmode causes problem with close() on Windows

by syphilis (Canon)
on Jun 17, 2013 at 10:10 UTC ( #1039321=note: print w/ replies, xml ) Need Help??


in reply to Win32: Setting a layer with binmode causes problem with close() on Windows

Does a simple binmode($fh); fail to do the right thing ?

Cheers,
Rob


Comment on Re: Win32: Setting a layer with binmode causes problem with close() on Windows
Download Code
Re^2: Win32: Setting a layer with binmode causes problem with close() on Windows (warnings)
by Anonymous Monk on Jun 17, 2013 at 10:28 UTC
    :) but what is it supposed to do? I add
    print join ' ', 1, PerlIO::get_layers($fh), "\n"; binmode($fh, ':raw:perlio') or warn "UHOH $! \n$^E \n$@\n "; print join ' ', 2, PerlIO::get_layers($fh), "\n"; binmode($fh, ':raw') or warn "UHOH $! \n$^E \n$@\n "; print join ' ', 3, PerlIO::get_layers($fh), "\n"; binmode($fh, ':raw:raw:raw:raw') or warn "UHOH $! \n$^E \n$@\n "; print join ' ', 4, PerlIO::get_layers($fh), "\n"; binmode($fh, ':raw:raw:raw:raw:unix:crlf') or warn "UHOH $! \n$^E \n$@ +\n "; print join ' ', 5, PerlIO::get_layers($fh), "\n"; for(1..3){ binmode($fh, ':raw:pop') or warn "UHOH $! \n$^E \n$@\n "; print join ' ', 6, PerlIO::get_layers($fh), "\n"; } #~ binmode($fh, ':raw:crlf:perlio') or warn "UHOH $! \n$^E \n$@\n "; binmode($fh, ':pop:pop:win32') or warn "UHOH $! \n$^E \n$@\n "; print join ' ', 6, PerlIO::get_layers($fh), "\n";
    and I get
    1 unix crlf 2 unix crlf perlio 3 unix crlf perlio 4 unix crlf perlio 5 unix crlf perlio unix crlf 6 unix crlf perlio unix 6 unix crlf perlio 6 unix crlf 6 win32
    in addition to the file-already-open

    if turning off unix and turning it on again, or turning it on twice, is wrong, perl should warn or die

    PerlIO seems thin, OTOH, the test suite lists a TODO #56644: PerlIO resource leaks on open() and then :pop in :unix and :stdio but its closed

    Any way you look at it there is nonsense around :)

Re^2: Win32: Setting a layer with binmode causes problem with close() on Windows
by rovf (Priest) on Jun 17, 2013 at 11:25 UTC
    I read again PerlIO. My mistake was certainly to regard :unix and :crlf as two alternative I/O layers, one doing the lineending translation in the Unix style (i.e. no translation necessary), and the other one in the Windows style. This is clearly wrong: :crlf is to be seen on top of </c>:unix</c>, the latter being the most elementary stlye.

    Indeed, just omitting binmode works; I can read both kinds of files on Windows.

    Now another, related question comes to my mind. How about creating files? When I want to create on Windows a file, which has Unix line endings, should I then

    • Pop the :crlf layer, or
    • Explicitly set the :raw layer, or
    • Just apply binmode without any layer
    , since just setting the layer to :unix shouldn't work either, for the same reason that it was nonsense when trying to read an Unix file on Windows. But which of these variants are reliably working, i.e. without nasty side effects which maybe come up much later? I guess all three of them are correct, but I'm not sure, and anyway, which one would you consider the preferable one?

    -- 
    Ronald Fischer <ynnor@mm.st>
      :) more options :)PerlIO::eol - PerlIO layer for normalizing line endings
      Text::FixEOL - Canonicalizes text to a specified EOL/EOF convention, repairing any 'mixed' usages
        Astonishing.

        Well, now I have learned that explicitly setting the :unix layer is, depending on the situation, either evil or useless, I have to say that I indeed found a case where it seems to work in the way I originally intended: When it comes to creating a file, and we leave out the possibilities I mentioned, and the ones you added, and write it in the :unix layer way, i.e.

        use strict; use warnings; open(my $fh,'>:unix','abc.txt') or die $!; print $fh "one line\n"
        we find that, after running this program on Windows, the line terminator of the file is LF (not CRLF). Different to reading a file, :unix layer seems to make sense here. Even unlinking the file works:
        use strict; use warnings; open(my $fh,'>:unix','abc.txt') or die $!; print $fh "one line\n" close $fh; # No error message on unlink unlink 'abc.txt' or die $!;
        However, this works only if we don't use binmode for setting the layer. The following fails:
        use strict; use warnings; open(my $fh,'>','abc.txt') or die $!; binmode($fh,':unix'); print $fh "one line\n" close $fh; # Error message on unlink unlink 'abc.txt' or die $!;
        This confirms the theory that the problem is not the setting of the :unix layer as such, nor the use of binmode for setting a layer, but the combination of both. Indeed, if we replace in the last example :unix by, say, :crlf or :raw, we don't get an error message.

        -- 
        Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039321]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-07-31 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls