in reply to Re: Using ":raw" layer in open() vs. calling binmode()
in thread Using ":raw" layer in open() vs. calling binmode()
...may not be appropriate (at least on windows).open(my $fh, '<', $filename); binmode($fh);
If you want to read raw bytes from a filehandle (for example to look for a BOM) and _then_ add an encoding layer then you need to ensure that there isn't a :crlf layer hanging around to begin with which the above has (it may not be enabled, but it's still there *). This is because of :crlf not working nicely with encodings eg. it breaks tell().
So, the method i use is:
* Consider:open(my $fh, "<:raw:perlio", $file); # read first few bytes for a BOM or ASCII in utf(16|32) # to determine encoding binmode $fh, ":encoding($encoding)"
gives:open $fh, "<", $^X or die; binmode $fh; print "1: ", join(' ', PerlIO::get_layers($fh, details => 1)), "\n"; close $fh; open $fh, "<:raw:perlio", $^X or die; print "2: ", join(' ', PerlIO::get_layers($fh, details => 1)), "\n"; close $fh;
1: unix 4195328 crlf 4195328 2: unix 4195328 perlio 4195328
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Using ":raw" layer in open() vs. calling binmode()
by Anonymous Monk on Jun 15, 2007 at 14:10 UTC | |
by Anonymous Monk on Jun 15, 2007 at 15:41 UTC |