J
J. Romano
Hi,
I've had a little Perl problem recently that I've been wondering if
there is a solution for.
I'm using ActiveState Perl for Win32, and I need to read in binary
files. I use the diamond operator in a while loop after setting slurp
mode (in order to read in the whole file at once). In other words, my
code looks something like this:
$/ = undef; # set "slurp" mode
while (<>) {
my $fileLen = length $_;
print "File \"$ARGV\" contains $fileLen bytes.\n";
}
With this script, someone could type
perl script.pl file1 file2 file3
and get output like:
File "file1" contains 15 bytes.
File "file2" contains 21 bytes.
File "file3" contains 133 bytes.
Now, I realize that I can find the size of a file by using the -s
filetest operator, but that's not what I want to do (I just printed
the file length as an example). Ultimately I want to peek into the
files and look at the values at specific bytes. But in order to do
this I have to make sure that the \n\r (or \r\n) combination doesn't
get converted to one character (I've been burned by this before).
So I need to set binmode() on these files, but how do I do it with
the diamond operator? The only immediate solution I can think of is
to re-write the code so that it opens and closes a filehandle, like
this:
foreach my $file (@ARGV) {
$/ = undef; # set "slurp" mode
open(FILE, $file) or die "Cannot read \"$file\": $!";
binmode(FILE);
$_ = <FILE>;
close(FILE);
my $fileLen = length $_;
print "File \"$file\" contains $fileLen bytes.\n";
}
This way I have to add four more lines of code and check if open was
successful. I can definitely do it this way, but if there is a
quicker way of using binmode() with the diamond operator, I'd like to
know about it.
So, does anyone know if it is possible to set binmode() when using
the diamond operator (specifically in "slurp" mode)?
Thanks in advance,
Jean-Luc
I've had a little Perl problem recently that I've been wondering if
there is a solution for.
I'm using ActiveState Perl for Win32, and I need to read in binary
files. I use the diamond operator in a while loop after setting slurp
mode (in order to read in the whole file at once). In other words, my
code looks something like this:
$/ = undef; # set "slurp" mode
while (<>) {
my $fileLen = length $_;
print "File \"$ARGV\" contains $fileLen bytes.\n";
}
With this script, someone could type
perl script.pl file1 file2 file3
and get output like:
File "file1" contains 15 bytes.
File "file2" contains 21 bytes.
File "file3" contains 133 bytes.
Now, I realize that I can find the size of a file by using the -s
filetest operator, but that's not what I want to do (I just printed
the file length as an example). Ultimately I want to peek into the
files and look at the values at specific bytes. But in order to do
this I have to make sure that the \n\r (or \r\n) combination doesn't
get converted to one character (I've been burned by this before).
So I need to set binmode() on these files, but how do I do it with
the diamond operator? The only immediate solution I can think of is
to re-write the code so that it opens and closes a filehandle, like
this:
foreach my $file (@ARGV) {
$/ = undef; # set "slurp" mode
open(FILE, $file) or die "Cannot read \"$file\": $!";
binmode(FILE);
$_ = <FILE>;
close(FILE);
my $fileLen = length $_;
print "File \"$file\" contains $fileLen bytes.\n";
}
This way I have to add four more lines of code and check if open was
successful. I can definitely do it this way, but if there is a
quicker way of using binmode() with the diamond operator, I'd like to
know about it.
So, does anyone know if it is possible to set binmode() when using
the diamond operator (specifically in "slurp" mode)?
Thanks in advance,
Jean-Luc