Raymond Wan wrote:
Hi Chas., Jenda,


On Mon, May 11, 2009 at 6:00 PM, Chas. Owens <chas.ow...@gmail.com> wrote:

On Mon, May 11, 2009 at 04:50, Jenda Krynicky <je...@krynicky.cz> wrote:
From: Raymond Wan <rwan.w...@gmail.com>
I'm on a Linux system too; I guess I've used it for so long, I  forgot
about
the situations when binary/text does matter (i.e., Windows).  I see...so
it
doesn't matter.  That would make  sense since I just pipe to stdout
right
now and whether I'm sending "text" [ie.,  human-readable characters] or
not,
it  all seems to work fine...
Well ... it seems, but it doesn't have to. Based on the locale
settings, if you do not binmode() the filehandle or open it with the
right IO layer specified, the stuff you print may undergo some
charset conversions.

perldoc -f binmode says

On some systems (in general, DOS and Windows-based 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. Also, people can set
their I/O to be by default UTF-8 encoded Unicode, not bytes.

In other words: regardless of platform, use binmode() on binary data,
like for example images.
snip

or the more modern:

open my $fh, ">:raw", $filename
   or die "could not open $filename: $!";

from perldoc perlio[1]
   The :raw  layer is defined as being identical to calling
   binmode($fh) - the stream is made suitable for passing
   binary data i.e. each byte is passed as-is. The stream
   will still be buffered.

1. http://perldoc.perl.org/PerlIO.html



I see.  I'm not writing image data, but my own data (sequence of 4-byte
integers), so I guess I should be using binmode anyway.

So, of the two (binmode  and :raw), the latter  is the newer/more modern
method?  With so many ways to do things in Perl,  I often don't know which
one is the more accepted one.

Or you could also use the open pragma:

perldoc open

Thank you! I'll be sure to use :raw, even if I'm just writing to stdout.


John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to