Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Error binmode() on unopened filehandle

by Marshall (Canon)
on May 03, 2020 at 13:49 UTC ( [id://11116376]=note: print w/replies, xml ) Need Help??


in reply to Error binmode() on unopened filehandle

Once you go to BINMODE on a file handle, a record separator makes no sense.
A more typical way to read a large binary file would be statements like this:
binmode INBIN; my $n_bytes = read(INBIN, $buff, $BUFSIZE);
That's a request to read $BUFSIZE bytes from INBIN into $buff. $n_bytes is number of bytes actually read from INBIN.
I've never wound up reading a binary file in a single shot into a Perl variable because my bin files are not appropriate for that (so big they need to be read in chunks). However, I guess this would work (untested):
use MIME::Base64; use strict; use warnings; my $upload_dir = 'uploaded_files'; my $file_name = 'image.jpg'; ### Open the image to convert to base64 open (IMAGE, '<', "$upload_dir/$file_name") or die "$!"; binmode IMAGE; my $binary = <IMAGE>; my $base64 = encode_base64(pack('B*', $binary)); print "base64 = $base64\n";

Replies are listed 'Best First'.
Re^2: Error binmode() on unopened filehandle
by jo37 (Deacon) on May 03, 2020 at 14:03 UTC
    Once you go to BINMODE on a file handle, a record separator makes no sense.

    Maybe you should have tested this.

    #!/usr/bin/perl use strict; use warnings; binmode DATA; my $binary = <DATA>; print "$binary\n"; __DATA__ first second
    This gives:
    first

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      Ok, I tested with a .jpg file I had laying around.
      use warnings; use strict; open (my $fh, '<', "COVID19-Death02Apr.jpg") or die "$!"; binmode $fh; my $binary = <$fh>; #entire binary file goes to Perl variable print '',$binary;
      Works just fine!

      What you are overlooking is that you must set binmode before reading from the file. The Perl DATA file handle is an open file handle to the Perl program source that is seeked to the first character on the next line after __DATA__. This is buffered character mode I/O. You can't switch modes in the midst of a file.

        You can't switch modes in the midst of a file.

        You sure can:

        use warnings;
        use strict;
        use utf8;
        use Data::Dumper;
        $Data::Dumper::Useqq=1;
        
        print join(", ",PerlIO::get_layers(*DATA)),"\n";
        print Dumper( scalar <DATA> );
        binmode DATA;
        print join(", ",PerlIO::get_layers(*DATA)),"\n";
        print Dumper( scalar <DATA> );
        
        __DATA__
        H∃llⓄ, 🗺!
        H∃llⓄ, 🗺!
        

        Output:

        unix, perlio, utf8 $VAR1 = "H\x{2203}ll\x{24c4}, \x{1f5fa}!\n"; unix, perlio $VAR1 = "H\342\210\203ll\342\223\204, \360\237\227\272!\n";

        I disagree. What about this:

        #!/usr/bin/perl use strict; use warnings; my $data = <<EOF; first second EOF open my $fh, '<', \$data; binmode $fh; my $binary = <$fh>; print "$binary\n";

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

        Works just fine!

        That will only read until the first 0A byte. You need local $/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found