Prev: FAQ 4.72 How do I determine whether a scalar is a number/whole/integer/float?
Next: FAQ 8.1 How do I find out which operating system I'm running under?
From: Peng Yu on 11 Jun 2010 06:43 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;
From: Tad McClellan on 11 Jun 2010 08:54 Peng Yu <pengyu.ut(a)gmail.com> wrote: > 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; 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. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: Peng Yu on 11 Jun 2010 11:10 On Jun 11, 7:54 am, Tad McClellan <ta...(a)seesig.invalid> wrote: > Peng Yu <pengyu...(a)gmail.com> wrote: > > 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; > > 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?
From: Tad McClellan on 11 Jun 2010 11:17 Peng Yu <pengyu.ut(a)gmail.com> wrote: > On Jun 11, 7:54 am, Tad McClellan <ta...(a)seesig.invalid> wrote: >> Peng Yu <pengyu...(a)gmail.com> wrote: >> > 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; >> >> 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. Use binmode on binary files. Do not use binmode on text files. (regardless of platform) > Does it > mean that I can not use binmode for text files? You can, but you probably don't want to. :-) So, you should not use binmode on 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? Yes. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: John Bokma on 11 Jun 2010 13:48
Tad McClellan <tadmc(a)seesig.invalid> writes: > 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'; -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development |