Perl5 リファレンス


       binmode FILEHANDLE
               Arranges for FILEHANDLE to be read or written in
               "binary" or "text" mode on systems where the run-
               time libraries distinguish between binary and text
               files.  If FILEHANDLE is an expression, the value
               is taken as the name of the filehandle.
               DISCIPLINE can be either of `":raw"' for binary
               mode or `":crlf"' for "text" mode.  If the
               DISCIPLINE is omitted, it defaults to `":raw"'.

               binmode() should be called after open() but before
               any I/O is done on the filehandle.

               On many systems binmode() currently has no effect,
               but in future, it will be extended to support
               user-defined input and output disciplines.  On
               some systems binmode() is necessary when you're
               not working with a text file.  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.

               In other words:  Regardless of platform, use
               binmode() on binary files, and do not use
               binmode() on text files.

               The `open' pragma can be used to establish default
               disciplines.  See the open manpage.

               The operating system, device drivers, C libraries,
               and Perl run-time system all work together to let
               the programmer treat a single character (`\n') as
               the line terminator, irrespective of the external
               representation.  On many operating systems, the
               native text file representation matches the
               internal representation, but on some platforms the
               external representation of `\n' is made up of more
               than one character.

               Mac OS and all variants of Unix use a single
               character to end each line in the external
               representation of text (even though that single
               character is not necessarily the same across these
               platforms).  Consequently binmode() has no effect
               on these operating systems.  In other systems like
               VMS, MS-DOS and the various flavors of MS-Windows
               your program sees a `\n' as a simple `\cJ', but
               what's stored in text files are the two characters
               `\cM\cJ'.  That means that, if you don't use
               binmode() on these systems, `\cM\cJ' sequences on
               disk will be converted to `\n' on input, and any
               `\n' in your program will be converted back to
               `\cM\cJ' on output.  This is what you want for
               text files, but it can be disastrous for binary

               Another consequence of using binmode() (on some
               systems) is that special end-of-file markers will
               be seen as part of the data stream.  For systems
               from the Microsoft family this means that if your
               binary data contains `\cZ', the I/O subsystem will
               ragard it as the end of the file, unless you use

               binmode() is not only important for readline() and
               print() operations, but also when using read(),
               seek(), sysread(), syswrite() and tell() (see the
               perlport manpage for more details).  See the `$/'
               and `$\' variables in the perlvar manpage for how
               to manually set your input and output line-
               termination sequences.