This just a very short FYI for anyone who is interested in why read() would refuse to read the "right" number of bytes from a file. In the following snippet I am slurping an entire file and verifying that I read the expected number of bytes. I was getting the error message "Read 3157 but expected 3158 bytes from 002971DD46D2CB2286256BAC002C26FB.xml" and it didn't immediately occur to me that because of the implicit new-line handling on text files, a single \r character had been removed and my $expected was differing from my $got value.
One application of binmode() later and my code worked. This isn't a revelation, perlfunc already documents this but since I had to think about it for moment today I figured I'd just share and let this be a reminder to anyone else who might get something out of it.
sub readfile { my $fname = shift; my $fdata; open XML, "<", $fname or die "Couldn't open $fname: $!"; binmode XML or die "Couldn't binmode $fname: $!"; my $expected = -s XML; my $got = read XML, $fdata, $expected; $expected == $got or die "Read $got but expected $expected bytes from $fname: $!"; close XML or die "Couldn't close $fname: $!"; return \ $fdata; }
In reply to Remember to binmode text files by diotalevi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |