How to encode string to Base64 with MIME::Base64 in Perl?
Question
How to encode string to Base64 with MIME::Base64 in Perl?
Encoding a string into Base64 is a common task when dealing with binary data, email attachments, or transmitting data over media that are designed to handle textual data. In Perl, the MIME::Base64 module from the core distribution provides easy-to-use functions to encode and decode Base64 strings.
Using MIME::Base64 to Encode a String
The MIME::Base64 module exports the encode_base64 function, which takes a plain string and returns its Base64-encoded equivalent. By default, it adds a newline every 76 characters (to conform with MIME standards), but you can suppress this behavior by passing an optional second argument.
Here's the basic approach:
use MIME::Base64;to include the module.- Call
encode_base64($string)to get the encoded string. - Optionally, suppress newlines with
encode_base64($string, "").
Understanding Perl Concepts in This Context
- Sigil: The
$sigil denotes a scalar variable (a single value like a string). - Context:
encode_base64returns a scalar in scalar context (the encoded string). - TMTOWTDI: Perl's “There's more than one way to do it” philosophy means you could also use other modules or write your own encoding—but
MIME::Base64is simplest and standard.
Common Pitfalls
- For multiline safe Base64 strings (typical in emails), allow the default behavior (newlines every 76 characters).
- To get a clean single-line Base64 string (for APIs, URLs, etc.), pass an empty string as the second argument.
- Remember to
use MIME::Base64;before calling the functions. encode_base64expects a string, not a file handle or binary stream—read binary data into a scalar first if needed.
Example Code
use strict;
use warnings;
use MIME::Base64;
# Original string to encode
my $string = "Hello, Perl Base64 encoding!";
# Encode with default line breaks (MIME standard)
my $encoded = encode_base64($string);
print "Encoded with line breaks:\n$encoded\n";
# Encode without any line breaks (single line)
my $encoded_single_line = encode_base64($string, "");
print "Encoded without line breaks:\n$encoded_single_line\n";
When you run this script, you should see output similar to:
Encoded with line breaks: SGVsbG8sIFBlcmwgQmFzZTY0IGVuY29kaW5nIQ== Encoded without line breaks: SGVsbG8sIFBlcmwgQmFzZTY0IGVuY29kaW5nIQ==
Because the encoded string is short, no line breaks appear by default in this example, but longer strings will show line breaks unless suppressed.
Version Notes
MIME::Base64 has been part of core Perl distributions since very early versions (at least Perl 5.6+), so no special installation is needed for most modern Perl interpreters (5.10+ and above).
In summary, just use MIME::Base64; and call encode_base64($string) to get a Base64 encoded string easily and reliably!
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 14ms
Encoded with line breaks:
SGVsbG8sIFBlcmwgQmFzZTY0IGVuY29kaW5nIQ==
Encoded without line breaks:
SGVsbG8sIFBlcmwgQmFzZTY0IGVuY29kaW5nIQ==
(empty)Was this helpful?
Related Questions
- How to use File::Slurp to read entire file into variable in Perl?
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?