Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello, Monks !
I'm relatively new to Perl and still in a phase of learning how things work. My question might sound stupid to you but I searched perldoc back and forth and googled through the web (including your great site) without finding an answer. So here's my question:
Short version: How do I portably and correctly open() a binary file ?
Long version or how I got all confused about that:
My PerlIO documentation states the following about the ":raw" layer:
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.
Having read that I thought the following two code fragments would be equivalent:
open(my $fh, '<', $filename); binmode($fh);
open(my $fh, '<:raw', $filename);
Because the second one is shorter (and I only have to check one function return value) I used that method in some scripts.
So far so good. A couple of days ago I discovered the PerlIO::get_layers() function and curious and naiv as I am I did the following:
$, = ' '; $\ = "\n"; open(FH, '<', '/dev/null'); print(PerlIO::get_layers(FH)); # --> unix perlio
As documented and expected this is the default layer stack on an UNIX system.
open(FH, '<', '/dev/null'); binmode(FH); print(PerlIO::get_layers(FH)); # --> unix perlio
So is this one. No problems yet, but look at the next one:
open(FH, '<:raw', '/dev/null'); print(PerlIO::get_layers(FH)); # --> unix
Big surprise (at least for a dummy like me): The ":perlio" layer mysteriously disappeared. And this mystery is the whole reason for this post.
Obviously, using ":raw" in open() is not the same as calling binmode() afterwards. Having no ":perlio" layer also means that the stream is unbuffered now, right ? (Which explains the results of this PerlIO Benchmark, which I found while searching for answers.)
Btw., My perl version is v5.8.4 but a friend of mine just confirmed the last code snippet on perl version v5.8.8.
Could someone please explain to me what I misunderstood so badly here. (Need not be a lengthy answer, a link or a hint to the right place in some Perl documentation would suffice.)
Thanks so much !
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using ":raw" layer in open() vs. calling binmode()
by jdalbec (Deacon) on Jun 14, 2007 at 02:32 UTC | |
|
Re: Using ":raw" layer in open() vs. calling binmode()
by jbert (Priest) on Jun 14, 2007 at 09:52 UTC | |
|
Re: Using ":raw" layer in open() vs. calling binmode()
by Util (Priest) on Jun 14, 2007 at 12:21 UTC | |
|
Re: Using ":raw" layer in open() vs. calling binmode()
by Anonymous Monk on Jun 14, 2007 at 19:46 UTC | |
by Anonymous Monk on Jun 15, 2007 at 09:37 UTC | |
by Anonymous Monk on Jun 15, 2007 at 14:10 UTC | |
by Anonymous Monk on Jun 15, 2007 at 15:41 UTC | |
|
Re: Using ":raw" layer in open() vs. calling binmode()
by jdalbec (Deacon) on Jun 14, 2007 at 23:30 UTC | |
by Anonymous Monk on Jun 15, 2007 at 15:43 UTC |