Hello. I am trying to convert an image to Base64. My environment is Windows 10, Perl 5.24.3

Thank you all in advance!

My code is...

use MIME::Base64; use strict; use warnings; my $upload_dir = 'uploaded_files'; my $file = 'image.jpg'; my $base64_a = ''; ### Open the image to convert to base64 open (IMAGE, "$upload_dir/$file") or die "$!"; binmode IMAGE; my $binary = do{ local $/ = undef; <IMAGE>; }; binmode $binary; $base64_a = encode_base64($binary); my $base64 = encode_base64(pack('B*', $binary)); print "base64 = $base64\n";

The funny thing is that it works but also kicks off an error.

The output is below... I truncated the Base64 output but this gives you an idea.

C:\inetpub\foto_root>test_binmode.pl binmode() on unopened filehandle at C:\inetpub\foto_root\test_binmode. +pl line 15, <IMAGE> line 1. base64 = oJwMowGRQNNgH8d24aQ2RsdiAAAAAAAAJLj8n+AVVkc4dN3p10iVUqqqqqqqp +VKpVKpVVVVU3/gV VmRYq3PyP5IelVVVVVVSqVSqVSqVSqVUZ1AAEGE3pUtAeRV1p247Xr9p2Guv7n9A9nOnOn +ZXdA42 pdQesc/9hOpZohX2DutSt/81SZ59Hb0FIO76iahG1+vNi8zyfmjNxSejvblU86wvDLZXRm +4ry0mW yua0S4Q90eqW7MYcDZcP4l9PBrvn+IDVzndz7jzr6Qdtxw8229DXCkbJBku40l+WPP3LWM +zb0n3R 1HH038+luuLCle7a0I1fbKViAAAbuACaCEXYx48/lGgW3nX3PuaVGfWYO6Hji+61TLusOy +q4VbUB yxV669JBpWOm9iflsw4PPuH19M39fAjGaXS9L5lTHpFLOgdiHgnWpHVBnhBcG1Ey7lUnsX +5m6E6a 7QvIKnZ4L/a6MrbXSao8wtaijRod4XP87X2ZlXqDVetDfQoJPWW2sWnVUfMUFeIYK3RVY+ +1kUolC ArLfmXFpHX/GYvYsydAm5e77Z9l6nO0v2Y3GjeNilVltW7uA0m1l42/tA7it9WBp2YC0RP +w86c8+

In reply to Error binmode() on unopened filehandle by RedJeep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.