I teach Perl to programmers using both *nix and Windows operating systems. One of the suggestions I give them is to use File::Temp for temporary files. It's a great module, especially because it's standard so everyone will already have it.
Unfortunately, I've just discovered that File::Temp doesn't exactly do what I want it to. When my students using *nix open temporary text files, everything just works. When my students using Windows open temporary text files, their newlines (\n) are not correctly translated into crlf.
John M. Dlugosz touched on the problem a couple of years ago in Why does File::Temp use sysopen? but he was blaming the wrong cause. File::Temp uses binmode (or an equivalent) if it can. It says so in the documentation.
The file returned by File::Temp will have been opened in binary mode if such a mode is available. If that is not correct, use the binmode() function to change the mode of the filehandle.
Note that you can modify the encoding of a file opened by File::Temp also by using binmode().
The binmode documentation says:
For the sake of portability it is a good idea to always
use it when appropriate, and to never use it when it isn't
I realise I can "fix" File::Temp's use of binmode for text files by calling binmode:
my ($tmp_fh, $tmp_name) = tempfile();
binmode($tmp_fh, ":crlf" );
but I don't want to teach that. I want an easy, 100% portable way of doing it. I want my students to be able to write exactly the same code on either system and have it work - even for text files.
Am I missing an easier way of doing this?
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||