in reply to Re^4: Error binmode() on unopened filehandle
in thread Error binmode() on unopened filehandle

I don't understand the point that you are trying to make. You open a file handle to a Perl var. That's fine. You set binmode before you read from that file handle and that's fine too.

Find some .jpg file you have somewhere and try the code that I posted. Use of the DATA file handle is "special".

Your initial premise that you could read binary data from the DATA file handle is wrong. That data will be in a character format.

Update: Here is a Perl program that reads and prints itself. DATA is an already read and opened file handle.

use warnings; use strict; print "testing seek of DATA handle\n"; print "this will print this program\n"; seek (DATA,0,0); my $text = do{ local $/ = undef; <DATA>; }; print $text; __DATA__ asdfasdf asdfasdf

Replies are listed 'Best First'.
Re^6: Error binmode() on unopened filehandle
by jo37 (Curate) on May 03, 2020 at 14:54 UTC

    My last example was without the special DATA file handle and shows that my $binary = <$fh> will read the data from $fh up to and including the first appearance of $/. Using binmode on a file handle does not change this behaviour.

    You may check if:

    • your data really does not contain a record separator
    • the generated base64 data, when decoded, resembles the original file

    EDIT: Here is another:

    #!/usr/bin/perl use strict; use warnings; use constant RANDFILE => "rand.dat"; system "dd if=/dev/urandom of=" . RANDFILE . " bs=1k count=64"; open my $fh, '<', RANDFILE or die; binmode $fh; my $binary = <$fh>; print "got: ", length($binary), "\n";

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re^6: Error binmode() on unopened filehandle
by ikegami (Patriarch) on May 04, 2020 at 03:27 UTC

    Your initial premise that you could read binary data from the DATA file handle is wrong.

    Works for me.

    use feature qw( say ); binmode DATA if $ARGV[0]; while (<DATA>) { say sprintf "%v02X", $_; } __DATA__ abc def
    >perl a.pl 0 61.62.63.0A 64.65.66.0A >perl a.pl 1 61.62.63.0D.0A 64.65.66.0D.0A