binmode for readling the whole file?

P

Peng Yu

To read a whole file into a string. The following webpage uses
binmode.

http://www.perlmonks.org/?node_id=1952

But the following code from perlvar doesn't. I tried both on linux.
They are identically. But if I want the raw data in the file, I should
always use binmode to be safe across all OS, right?

open my $fh, "<", "foo" or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
close $fh;
 
P

Peng Yu

The issue regarding whether or not to use binmode has nothing to
do with how much of the file you plan to read, either full or partial.

This issue is as described in the documentation for binmode:

     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
     data, like for example images.

It is the *contents* of the file that dictate whether or not
binmode() should be used.

I had read the document. But I don't what 'appropriate' means. Does it
mean that I can not use binmode for text files?

My understand is that in linux, it doesn't matter whether binmode is
used or not for text files. But windows, binmode should not be used
for text files, because of the conversion of the CR and LF character.
Is my understand correct?
 
J

John Bokma

Tad McClellan said:
Use binmode on binary files.

Do not use binmode on text files.

/unless/ you want the data as it is on disk, for example to
calculate a check sum.

Another reason to use binmode might be if some code has opened the file
for you and you want to use a different layer, e.g.

binmode $fh, ':utf8';
 
S

sln

/unless/ you want the data as it is on disk, for example to
calculate a check sum.
Semantics! You wouldn't create a check sum on a text file.
Otherwise it wouldn't be a text file, it would be
a binary file.

-sln
 
J

John Bokma

Semantics! You wouldn't create a check sum on a text file.
Otherwise it wouldn't be a text file, it would be
a binary file.

Is that so? So if I calculate a MD5 digest over a text file to check its
integrity it magically becomes a binary file? Even if I add the checksum
to the end of the text file, it still can be a text file.
 
U

Uri Guttman

JB> Is that so? So if I calculate a MD5 digest over a text file to check its
JB> integrity it magically becomes a binary file? Even if I add the checksum
JB> to the end of the text file, it still can be a text file.

it matters on winblows. if you just open a text file with no binmode and
pass the handle to a checksum sub, it will not calculate a proper sum
for the file itself. the cr/lf pairs will become newlines and the sum
will be different than if done on the raw file.

uri
 
S

sln

Is that so? So if I calculate a MD5 digest over a text file to check its
integrity it magically becomes a binary file? Even if I add the checksum
to the end of the text file, it still can be a text file.

Haha! Its funny but after you validate the integrity with a checksum, it
reads different when read in text mode depending on the OS.
Semantics..

-sln
 
S

sln

I create checksums on text files fairly often.

Oh yeah, for what? Spend a lot of time on it do ya?
Nonsense!

???? You mean no sense. Its semantics..
What makes you think that taking a checksum changes a file's type?

What is a file type and how do you change it?
Oh yeah, you create checksums on text files all the time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top