jy38 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to read binary data out of a file and into a string.
local $/=undef; open FP, "<fileName" or die "Cannot open file!\n"; my( $myFile ) = <FP>; close FP;
Whenever this runs into the ascii character 0x1A Perl seems to interpret this as the end of the data and the string will not contain the entire contents of the file. How can I avoid this? --John

Replies are listed 'Best First'.
Re: Reading binary data
by Fastolfe (Vicar) on Jan 30, 2001 at 06:14 UTC
    0x1a = \cZ, or a DOS end-of-file marker. It sounds like you're running under DOS/Win32. Try using binmode on the filehandle.
Re: Reading binary data
by dws (Chancellor) on Jan 30, 2001 at 05:52 UTC
    There's a sublety to the use of $/;

    Setting it side-effects the currently selected file handle. Move it until after the open, or try Ignore that. I wasn't sufficiently under the influence of caffiene, and got $/ confused with $|.

    Try

    open FP, "<filename" or die "$filename: $!"; binmode(FP); my $myFile = do { local $/; <FP> }; close FP;
    local $/; is shorthand for local $/ = undef;

    I threw in the binmode in case you're running on Win32.