Call: (480) 382-8464

Perl MIME::Base64

Base64 Encoding allows you to convert a binary file into a text message. Sometimes binary is not allowed to be transmitted. Whatever your reason, here is how you create a Base64 encoded file using Perl:

Base64 Encoding with Perl

I didn’t write this, but thought it was worth saving. is an archive for my own personal use, I hope it helps others too.

Perl MIME::Base64

use strict;
use warnings;

# credit:

use MIME::Base64;
use Getopt::Std;

sub usage {
my $usage = q{

Encodes or decodes base64 files to or from binary files.
usage: [-d|-e] <input file> [-o] 

    decoding: -d temp.txt -o out.txt
    encoding: -e temp.txt -o out.txt

    return $usage;

sub main {
    my %opts;
    # Get command-line options. We want the user
    # to specify an input file (either for encoding
    # or decoding) and an output file.
    getopts('e:d:o:', \%opts);
    # User must enter either -e  or
    # -d 
    unless($opts{e} or $opts{d}) {
        print "\nNo input file specified\n";
        die usage();
    # User must specify -o  and hasn't.
    unless($opts{o}) {
        die "\nNo output file specified\n";
    # Now we've got an output file name.
    my $output = $opts{o};
    # The input file name is after -e or -d.
    my $input = $opts{e} || $opts{d};
    # Open the input file.
    unless(open INPUT, $input) {
        die "\nUnable to open input file '$input'\n";
    unless(open OUTPUT, '>'.$output) {
        die "\nUnable to create output file '$output'\n";
    # Stop text mode mangling our files.
    binmode INPUT;
    binmode OUTPUT;
    # Undef the file record separator so we can read the
    # whole thing in one go.
    undef $/;
    # Read the input file.
    my $contents = <INPUT>;
    # Create the output file, encoding or decoding as
    # specified by the user.
    if($opts{e}) {
        print OUTPUT encode_base64($contents);
        print "Encoded '$input'; results in '$output'\n";
    elsif($opts{d}) {
        print OUTPUT decode_base64($contents);
        print "Decoded '$input'; results in '$output'\n";
    close INPUT;
    close OUTPUT;


Leave a Reply

Your email address will not be published. Required fields are marked *

Main Offers/Services

Hosting and website related services offered by Digital Crunch:

Managed VPS Hosting

Server Updates, Module/Software Installs, Firewall, Security, Hosting Management, Wordpress - let us manage it for you

Hosted Email

Setup and Maintain Business Class, Secure Email Hosting including spam protection and 25GB of storage space

Linux Consulting

Server Updates, Module/Software Installs, Firewall, Security, Hosting, Wordpress and Apps, anything related to Linux

Email Marketing Services

We broadcast your content to your list, capture leads for you, follow up on schedules, make more sales for you

Website Maintenance

Content updates, menu changes, graphics changes, adding content, getting links, building traffic, building sales funnels

Hosting Tutorials

Tutorials we've written for other customers that like to get their hands dirty and learn about hosting