Category: Miscellaneous
Author/Contact Info Mephit a.k.a. J. Kufrovich
Description: "Better" than the uudecode that comes with Linux, anyway. This script that will take a text file filled with uuencoded data (even multiple files, and with non-data as well, as if saved from Usenet) and uudecode each file found. Useful for me, anyway. Uses Convert::UU. Comments or questions? Leave a message at the beep.
#!/usr/bin/perl -w

# Thanks to folks on comp.lang.perl.misc for a few tips.

use strict;
use Convert::UU qw/uudecode/;

die "Filename required\n" unless $ARGV[0];
my $file = shift;
print "Parsing $file ...\n\n";
open (FILE, "<", $file) or die "Can't open file for reading:  $!\n";

my ($data, $tempfile, $tempmode);

MAIN: while (<FILE>) {
  next MAIN unless /^begin / .. /^end\s+/;
    # extract the filename.  If we already have a filename, it's a pos
+sible short file in the data file, so reset everything. Could all of 
+this be done better somehow?
    if ( /^begin / ) { 
      if ($tempfile) {
    print "Warning: possible short file $tempfile found\n\n";
    $data = '';
      } # if ($tempfile)
      ($tempmode, $tempfile) = ( /\Abegin (\d+) (.*)$/m);      
      last INNER;
    } # if ( /^begin/ )
    last INNER if /^end\s+/;     # Last line of data.
    next MAIN if /[a-z]/;        # Skip non-data lines
    next MAIN if /^\s+$/;        # And again
  } #end of INNER

  $data .= $_;

  if ( /^end\s+/ ) {
    my $filename = $tempfile;
    my $mode = $tempmode;
    print "Found uuencoded file $filename\n";

    my $outstr = uudecode($data);
    if (-e $filename) {
      print "Error, $filename already exists.  Ignoring\n\n";
      $data = '';
      $tempfile = '';
      $filename = '';
      next MAIN;
    open (OUTFILE, ">", $filename) 
      or die "Can't open $filename for writing:  $!\n";
    print "Writing to $filename ...\n\n";
    print OUTFILE $outstr;
    close OUTFILE;

    system ("chmod", $mode, $filename) 
      and warn "Unable to change perms of $filename:  $!\n";

    $data = '';
    $tempfile = '';
    $filename = '';
close (FILE)


Code reposted on 4-27-02 so it doesn't appear to be double-spaced when viewed without code wrapping (I hope).

Bah! Updated again on 5-13-02, this time in Netscape, which hopefully won't insert extra ^Ms, like Opera does, apparently.