How to decode Base64 string in Perl using MIME::Base64?
Question
How to decode Base64 string in Perl using MIME::Base64?
Decoding a Base64-encoded string in Perl is straightforward with the core module MIME::Base64, which provides easy-to-use encoding and decoding functions. Base64 encoding converts binary data into ASCII characters, commonly used for embedding binary data (like images or certificates) in text formats such as emails or XML. Decoding it back to raw binary or text is often needed in scripts processing such data.
Using MIME::Base64 to Decode Base64
The MIME::Base64 module is included in Perl’s core distribution, so no extra installation is typically needed. The main function for decoding is decode_base64, which takes a Base64 string and returns the decoded binary data.
Keep in mind Perl’s context and sigils:
$denotes a scalar variable (single string/number).@and%are arrays and hashes, respectively.- The
decode_base64function returns a scalar string containing decoded data.
Simple Example
Below is a complete, runnable Perl script demonstrating decoding a Base64 string into its plain text equivalent and printing the output to STDOUT:
use strict;
use warnings;
use MIME::Base64;
# Base64 encoded string (example: "Hello, Perl!")
my $base64_string = 'SGVsbG8sIFBlcmwh';
# Decode the Base64 string to original text
my $decoded = decode_base64($base64_string);
print "Base64 input: $base64_string\n";
print "Decoded output: $decoded\n";
When you run this script, it will print:
Base64 input: SGVsbG8sIFBlcmwh
Decoded output: Hello, Perl!
Key Points and Common Pitfalls
- Trailing newlines: If your Base64 input has embedded newlines or spaces (common when copied from emails),
decode_base64handles most cases but clean input is preferred. - Binary data: The decoded data might contain non-printable binary bytes. For binary data, do not print directly; instead, use proper binary file handling.
- Perl versions:
MIME::Base64has been in Perl core since Perl 5.7.3, so it should be available in all Perl 5 installations you find in use. - Context:
decode_base64works in scalar context and returns a scalar string. - Encoding issues: If the decoded data is a UTF-8 string, you may need to handle character encoding explicitly with
Encodemodule to avoid mojibake in some contexts.
Summary
To decode Base64 with Perl, just use MIME::Base64 and call decode_base64() on your input string. This module is efficient, included in core Perl, and well-documented. It exemplifies Perl’s TMTOWTDI (There’s More Than One Way To Do It) philosophy by offering both encoding and decoding in the same package.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 10ms
Base64 input: SGVsbG8sIFBlcmwh
Decoded output: Hello, Perl!
(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?