[comp.lang.perl] Very Slow Uudecode in Perl...

spike@world.std.com (Joe Ilacqua) (07/16/90)

	This is uudecode in perl.  Very very slow, but it does deal
with multi part uuencode files and strips out junk like mail headers.
My first perl program.  Care to make it faster?

#!/usr/local/bin/perl

while (<>) { 
  if ($seq = /^begin/ .. /^end/) {
    if (/^begin/) {
      ($junk,$mode,$file) = split(' ');
      $in = open(OUTPUT,">$file") || die "uudecode: can't open: $file $!\n";
      chmod $mode $file;
      next;
    }
    if (/^end/) {
      close OUTPUT;
      next;
    }
# uuencode puts 3 8 bit bytes in to four 7 bit chars like this:
# 000 011 112 222  The first character of each line is number of encoded
# bytes on the line, normally 45 = M


     if ((/[^a-z ]/) && 
	  (($bytes = (ord(substr($_,0,1)) - ord(" ")) & 077) ==
	   (length($_) - 2) * 3 / 4)) {
      $line = $_;
      $i = 1;
      while ($bytes != 0) {
	$A = (ord(substr($_,$i,1)) - ord(" ")) & 077;
	$B = (ord(substr($_,$i+1,1)) - ord(" ")) & 077;
	$C = (ord(substr($_,$i+2,1)) - ord(" ")) & 077;
	$D = (ord(substr($_,$i+3,1)) - ord(" ")) & 077;
	$a = ($A << 2) | ($B >> 4);
	$b = ($B << 4) | ($C >> 2);
	$c = ($C << 6) | $D;
	$bytes -= 3;
	$i += 4;
	$out = pack('ccc',$a,$b,$c);
	print OUTPUT "$out";
      }
    }
  }
}

->Spike (Spike@World.STD.com)
-- 
"The World" - Public Access Unix - +1 617-739-9753  24hrs {3,12,24}00bps