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

Hello, I am running on an AIX box 5.3. I am trying to run in the tcsh shell an encode on a pdf file and later on run a decode to re-establish the pdf. When I run this command:
perl -MMIME::Base64 -le 'print $MIME::Base64::VERSION;'I get the response: 3.07
Would you help me in figuring out what format should my Base64 command take to perform the encode and the decode activity, I tried something like:
 perl -MMIME::Base64 encode -f mime_multi_image.pdf -o gggf
I just do not have the format right, can you help?
Thanks

Replies are listed 'Best First'.
Re: Base64 encode and decode
by zentara (Cardinal) on Feb 26, 2009 at 15:06 UTC
    Play with this, do you really need a 1-liner?
    #!/usr/bin/perl use strict; use MIME::Base64 qw( encode_base64 ); #encode open INFILE, '<', $ARGV[0]; binmode INFILE; open OUTFILE, '>', $ARGV[1]; my $buf; while ( read( INFILE, $buf, 4096 ) ) { print OUTFILE encode_base64($buf); } close OUTFILE; close INFILE; ################################################### #decode_base64.pl: #!/usr/bin/perl use strict; use MIME::Base64 qw( decode_base64 ); open INFILE, '<', $ARGV[0]; open OUTFILE, '>', $ARGV[1]; binmode OUTFILE; my $buf; while ( $buf = <INFILE> ) { print OUTFILE decode_base64($buf); } close OUTFILE; close INFILE;

    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
      Thanks, these work great!.
      your help was greatly appreciated
Re: Base64 encode and decode
by dermoth (Initiate) on Sep 22, 2015 at 12:26 UTC
    I know this isn't a perl answer at all, but since you're looking for a oneliner how about using OpenSSL? From experience it's often available on many platforms.
    $ file datafile datafile: PNG image data, 640 x 480, 8-bit colormap, non-interlaced $ openssl base64 -e -in datafile >datafile.b64 $ file datafile.b64 datafile.b64: ASCII text $ openssl base64 -d -in datafile.b64 >datafile2 $ md5sum datafile datafile2 c8f00e2b29d5b516d2c2afd8e41dcdfd datafile c8f00e2b29d5b516d2c2afd8e41dcdfd datafile2
    For all the options, see openssl base64 -h. I used to use openssl base64 to copy-paste small binary files between multiple terminal windows (using stdin/out rather than file for output/input), as doing it from the network was requiring multiple hops through intermediate servers. -- Thomas
Re: Base64 encode and decode
by JavaFan (Canon) on Feb 26, 2009 at 15:06 UTC
    Did you try "man encode", as you seem to have an "encode" utility?
      Hi,
      . Here is what I get:
      man encode Manual entry for encode not found or not installed.
      I have seen the man pages for this on the web, and I have tried to execute
      uuencode -m mime_multi_image.pdf outfile > outfile
      it seems to run fine, output goes to outfile.
      Thanks!
Re: Base64 encode and decode
by moritz (Cardinal) on Feb 27, 2009 at 12:19 UTC
    If you have the unix utility recode installed, you can encode or decode your file in-place:
    # encoding recode char../b64 $file # decoding recode /b64..char $file
Re: Base64 encode and decode
by codeacrobat (Chaplain) on Feb 27, 2009 at 12:04 UTC
    MIME::Base64 oneliners to rescue.
    # encode perl -MMIME::Base64 -0777ne 'print encode_base64($_)' bla.pdf > bla.pd +f.encoded # decode perl -MMIME::Base64 -0777ne 'print decode_base64($_)' bla.pdf.encoded +> bla.pdf

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});