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

Greetings monks!

Will I be able to find an alternative to the MIME::Base64 module for encoding/decoding small text strings (using pure perl without requiring a loadable module?)

My dilemma is this: I need to encode/decode base64 on a free webserver that features perl but no MIME::Base64 module (and no gcc access). I tried copying the Base64.pm and the required Base64.so (from my home server) but it won't run due to version mismatch

Thanks for any tips!

Replies are listed 'Best First'.
Re: need base64 utility in pure perl
by tirwhan (Abbot) on Jan 19, 2006 at 16:04 UTC

    How about MIME::Base64::Perl? For quick questions like this it's always a good idea to try the chatterbox first BTW (or indeed a CPAN Search ;-).


    There are ten types of people: those that understand binary and those that don't.
      ah, thats EXACTLY what I needed. THANKS! No point in writing the code myself if its already there. I'll make a note to try chatterbox next time.
Re: need base64 utility in pure perl
by Limbic~Region (Chancellor) on Jan 19, 2006 at 16:17 UTC
    redss,
    ...on a free webserver that features perl but no MIME::Base64 module...

    This seems odd to me. MIME::Base64 has been a core module since 5.007003. Are you saying that they have intentionally removed the core modules or the perl is still of the 5.6.x variety? As has already been noted, there is a pure perl version available from CPAN

    Cheers - L~R

      Well apparently they've removed it, or maybe I just don't have access to that path. How can I tell which is the case?

      I do have a telnet account, but I dont have permission for many things like uname.. the perl version is v5.6.1...

      here is the output when I run the script:

      Can't locate MIME/Base64.pm in @INC (@INC contains: /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux /usr/lib/perl 5/site_perl/5.6.1 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.6.1/i386 -linux /usr/lib/perl5/vendor_perl/5.6.1 /usr/lib/perl5/vendor_perl .) at ./foo.pl line 2.

        redss,
        In addition to many other improvements, the versioning scheme also changed after 5.6.x. 5.007003 is really 5.7.3 which is > than 5.6.1 so it was not yet included in the version running there. It is unfortunate you are apparently stuck with a Perl about 5 years old.

        Cheers - L~R

Re: need base64 utility in pure perl
by ambrus (Abbot) on Jan 19, 2006 at 16:35 UTC
Re: need base64 utility in pure perl
by Fletch (Bishop) on Jan 19, 2006 at 16:01 UTC

    It shouldn't be that hard to take the spec (sec 6.8 of the RFC) and reimplement it yourself in pure Perl. You can even look at the source for MIME::Base64 for an actual code implementation.

Re: need base64 utility in pure perl
by zentara (Cardinal) on Jan 19, 2006 at 19:53 UTC
    #!/usr/bin/perl my $time = time; print "$time\n"; my $enc64 = EncodeBase64($time); print "$enc64\n"; my $dec64 = DecodeBase64($enc64); print "$dec64\n"; sub EncodeBase64 { my $s = shift ; my $r = ''; while( $s =~ /(.{1,45})/gs ){ chop( $r .= substr(pack("u",$1),1) ); } my $pad=(3-length($s)%3)%3; $r =~ tr|` -_|AA-Za-z0-9+/|; $r=~s/.{$pad}$/"="x$pad/e if $pad; $r=~s/(.{1,72})/$1\n/g; $r; } sub DecodeBase64 { my $d = shift; $d =~ tr!A-Za-z0-9+/!!cd; $d =~ s/=+$//; $d =~ tr!A-Za-z0-9+/! -_!; my $r = ''; while( $d =~ /(.{1,60})/gs ){ my $len = chr(32 + length($1)*3/4); $r .= unpack("u", $len . $1 ); } $r; }

    I'm not really a human, but I play one on earth. flash japh