Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 appropriate.

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?

In reply to Temporary text files with File::Temp by jarich

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2017-12-16 10:18 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (449 votes). Check out past polls.