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

Hi,I'm new to use Base64.The different results of encoding
between MIME::Base64 and MIME::Lite puzzle me.
use MIME::Lite; $msg = new MIME::Lite Type =>'image/gif', Path =>'d:/forum.gif', Filename =>'forum.gif'; $msg->print;
encoded results by MIME::Lite:
R0lGODlhGAAYAMT/AP///+/v79/f38/Pz7+/v6+vr5+fn8DAwI+Pj4CAgHx8 fHNzc3BwcGlpaWBgYF9fX1BQUENDQ0BAQDo6OjAwMCYmJiAgIB0dHRoaGhAQ EAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAcALAAAAAAYABgAQAXT4CGO5NEs ZTo+VAC8r4BIWp29hhYhCkYkloSLIJEQcrXkQ0V6OAABRAIhiDkqKNGiYoQV aggYLqksbbuBBOO7ZDKdVqx79XwlMpJXzQATUCIHLC4wAwN8YmM1EwcREkNF BIgASGRtB2eRaUGDDmVzJGcUlp8pTldZpHSIAqefcAAWNQlFA3GoIg8QknY2 MAYXCLgtiAwURUUQtZQRDxaDLwhgkpQachOzXVDPiZUHCBEZQgBE2ZNkNZYT FJlA4kTG3SqYUO2d6KmXXJmi+CkLDZ9CAAA7

use MIME::Base64 qw(encode_base64); open(FILE, "d:/forum.gif") or die "$!"; while (read(FILE, $buf, 1024)) { $file.=$buf} print encode_base64($file);
encoded results by MIME::Base64:
R0lGODlhGAAYAMT/AP///+/v79/f38/Pz7+/v6+vr5+fn8DAwI+Pj4CAgHx8fHNzc3BwcGlpaWBg YF9fX1BQUENDQ0BAQDo6OjAwMCYmJiAgIB0dHQ==

Why is the latter a part of the former?Can I get the whole
results using MIME::Base64 and how?
Thanks for any your help!^_^

Replies are listed 'Best First'.
RE: about MIME::Base64
by spudzeppelin (Pilgrim) on Aug 24, 2000 at 19:40 UTC

    I'm not sure why you're getting such unpredictible behavior, but if you do a perldoc on MIME::Base64 and check out the examples, you'll see a couple of neat tricks. My favorite is the "if you have enough memory" option:

    use MIME::Base64 qw(encode_base64); local($/) = undef; # slurp print encode_base64(<STDIN>);

    Spud Zeppelin * spud@spudzeppelin.com

RE: about MIME::Base64
by tenatious (Beadle) on Aug 25, 2000 at 05:23 UTC
    Try setting the buffer size to 57 or a multiple of 57.
      Thank you,But No effect.I had read the documents on
      MIME::Base64 coming with Perl5.6 before I posted.Sure,I
      have tried to use 57*n ...,but the results was same with
      without it at all!
      keep puzzled!:(
        I don't know what the problem is. I looked through MIME::Lite, and the length of the strings that are encoded is set to 45. So I threw together the following program using a 35k jpeg to test. Maybe the problem is the file is too short?
        #!/usr/bin/perl use MIME::Base64; use MIME::Lite; open FILE, "jpg/attachment-start.jpg"; while (read (FILE,$buf,45)) { $added .= encode_base64($buf); } open OUT1, ">blah1"; print OUT1 $added; close FILE; close OUT1; $msg = MIME::Lite->new(From => "me\@myhost.com", To => "blah\@blah.com", Type=> "image/jpg", Encoding =>"base64", Path => "jpg/attachment-start.jpg"); $message = $msg->as_string; open OUT2, ">blah2"; print OUT2 $message; close OUT2;
        then I ran it, and deleted the header... yes, I'm sure I should have used $message = $msg->body_as_string or something like that, but I didn't.

        then i did the following. ~$ diff blah1 blah2

        There was no output. It works for a file size of 35k. Perhaps your file is too short?

        Update: I slept on it and remembered that I left something out. If you set the "paranoid" flag in MIME::Lite, it will use it's own encoding scheme instead of calling MIME::Base64:encode_base64, as it does default, so this kind of behavior is really strange. The only difference in the way you are calling it and the way MIME::Lite calls it is that it calls it with a buffer size of (read (FH,$buf,45)) rather than (read (FH,$buf,1024)). So I stand by my earlier post... try setting the buffer size to 57*1.