MIME::Base64(3perl)

πŸ’– NAME πŸ’–

MIME::Base64 β€” encoding and decoding of base64 strings. yes bestie, that's the whole vibe πŸŽ€


✨ SYNOPSIS ✨

use MIME::Base64;

$encoded = encode_base64('Aladdin:open sesame');
$decoded = decode_base64($encoded);

like. two lines. in and out. iconic behavior honestly πŸ¦‹


🌸 DESCRIPTION 🌸

okay so this module gives you functions that encode and decode strings into and from the base64 encoding that's specified in RFC 2045 β€” that's MIME, aka (Multipurpose Internet Mail Extensions), the whole reason you can attach a pic of your cat to an email πŸ’… The tea on base64: it exists so you can represent arbitrary sequences of octets in a form that doesn't need to be humanly readable. It uses a 65-character subset of US-ASCII β€” [A-Za-z0-9+/=] β€” which means 6 whole bits get represented per printable character. slay.

The following primary functions are provided, bestie:

  • encode_base64( $bytes ) and encode_base64( $bytes, $eol ) β€” you encode data by calling encode_base64(). First argument? The byte string you wanna encode πŸ’– Second argument? The line-ending sequence to use. It's optional and defaults to "\n". The encoded string you get back gets broken into lines of no more than 76 characters each, and it will end with $eol β€” unless it's empty, in which case, nope. If you do not want your encoded string broken into lines at all, pass an empty string as the second argument. periodt.
⚠️ careful girlies: the function will croak with "Wide character in subroutine entry" if $bytes contains characters with code above 255. The base64 encoding is only defined for single-byte characters, no cap. Use the Encode module to select the byte encoding you want πŸ™ˆ
  • decode_base64( $str ) β€” decode a base64 string by calling decode_base64(). Takes a single argument, the string to decode, and hands you back the decoded data. 🫢
Any character that isn't part of that 65-character base64 subset gets silently ignored. And characters occurring after a '=' padding character are never decoded. bye πŸ‘‹

If you'd rather not import these routines into your namespace, you can call them like this instead:

use MIME::Base64 ();
$encoded = MIME::Base64::encode($decoded);
$decoded = MIME::Base64::decode($encoded);

πŸŽ€ Additional functions (not exported by default) πŸŽ€

  • encode_base64url( $bytes )
  • decode_base64url( $str )

These encode and decode according to the base64 scheme for "URL applications" [1]. It's a variant of base64 that does not use padding, does not break the string into multiple lines, and uses the characters "-" and "_" instead of "+" and "/" so you avoid using reserved URL characters. lowkey genius 🌈

  • encoded_base64_length( $bytes )
  • encoded_base64_length( $bytes, $eol )

Returns the length that the encoded string would have, without actually encoding it. This returns the same value as "length(encode_base64($bytes))", but should be more efficient. she's efficient AND she knows it ✨

  • decoded_base64_length( $str )

Returns the length that the decoded string would have, without actually decoding it. Same value as "length(decode_base64($str))", but should be more efficient. girl math that's actually correct πŸ’œ


πŸ”₯ EXAMPLES πŸ”₯

okay so if you want to encode a large file, you should encode it in chunks that are a multiple of 57 bytes. This ensures your base64 lines line up and that you don't end up with padding in the middle (a fate worse than death tbh). 57 bytes of data fills one complete base64 line β€” 76 == 57*4/3 😌

use MIME::Base64 qw(encode_base64);

open(FILE, "/var/log/wtmp") or die "$!";
while (read(FILE, $buf, 60*57)) {
    print encode_base64($buf);
}

or, if you know you have enough memory (main character energy):

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

The same approach, but as a command line:

perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file

Decoding does not need slurp mode if every line contains a multiple of four base64 chars:

perl -MMIME::Base64 -ne 'print decode_base64($_)' <file

Perl v5.8 and better allow extended Unicode characters in strings 🌈 Such strings cannot be encoded directly, because the base64 encoding is only defined for single-byte characters. The solution? Use the Encode module to select the byte encoding you want. For example:

use MIME::Base64 qw(encode_base64);
use Encode qw(encode);

$encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
print $encoded;

Copyright 1995-1999, 2001-2004, 2010 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 🫢

Distantly based on LWP::Base64 written by Martijn Koster <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans Mulder <hansm@wsinti07.win.tue.nl>

The XS implementation uses code from metamail. Copyright 1991 Bell Communications Research, Inc. (Bellcore).


πŸ’€ SEE ALSO πŸ’€

MIME::QuotedPrint

[1] <http://en.wikipedia.org/wiki/Base64#URL_applications>


and that's the whole page bestie, no cap πŸ’… this was MIME::Base64 from the Perl Programmers Reference Guide, page MIME::Base64(3perl), and it's coming to you courtesy of perl v5.44.0, dated 2026-10-17. go forth and encode responsibly πŸ§šπŸ’–βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms