[comp.lang.perl] Uudecode in Perl?

roy@cs.umn.edu (Roy M. Silvernail) (09/02/90)

Has anyone implemented UUdecode in Perl?

I'm working on a Perl project (targeted at MS-DOS) that will include
UUdecoding files. In the interest of conserving disk overhead, I'd
rather not create a temp file and spawn UUdecode. I'd rather just
accomplish the whole project in Perl. If a Perl UUdecode already has
been written, it would save me re-inventing a wheel.

Just another Perl newbie...
--
Roy M. Silvernail |+|  roy%cybrspc@cs.umn.edu  |+| #define opinions ALL_MINE;
main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
"This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me

composer@chem.bu.edu (Jeff Kellem) (09/02/90)

In article <VX6uo4w162w@cybrspc>
	cybrspc!roy@cs.umn.edu (Roy M. Silvernail) writes:
 > Date: 2 Sep 90 09:09:06 GMT
 >
 > Has anyone implemented UUdecode in Perl?

As of version 3.0 patchlevel 28 of perl, uu{de,en}coding is builtin to perl
via the `pack' and `unpack' functions.  Actually, it's in there at patchlevel
27, but pl28 came out quickly afterwards fixing some problems in perl, so you
should be at pl28.  ;-)  Here's a quick example of uudecode in perl using
those features.  Implementing uuencode would follow along similar lines.

As Larry said in the patch comments, it was a "wacked out evening" when he
added this feature.  :-)

Enjoy...

			-jeff

Jeff Kellem
Internet: composer@chem.bu.edu

p.s. It's just something quickly thrown together.. something can always be
done better.

#!/usr/bin/perl
#
# quick uudecode -- no error checking

($version,$patchlevel) = $] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
die "Uu{de,en}coding not builtin to perl till version 3.0 pl27.\n"
    if $version * 1000 + $patchlevel < 3027;

while (<>)
{
    if (/^begin\s(\d+)\s(.*)/)
    {
	$file = $2;
	$mode = "0" . $1;
	open (INPUT, "> $file") || die "Can't create $file: $!\n";
	chmod oct($mode), $file;
	next;
    }
    close (INPUT), next if /^end/;

    $line = unpack("u",$_);
    print INPUT $line;
}

merlyn@iwarp.intel.com (Randal Schwartz) (09/03/90)

In article <VX6uo4w162w@cybrspc>, cybrspc!roy@cs (Roy M. Silvernail) writes:
| Has anyone implemented UUdecode in Perl?
| 
| I'm working on a Perl project (targeted at MS-DOS) that will include
| UUdecoding files. In the interest of conserving disk overhead, I'd
| rather not create a temp file and spawn UUdecode. I'd rather just
| accomplish the whole project in Perl. If a Perl UUdecode already has
| been written, it would save me re-inventing a wheel.

It's nearly trivial in pl28...

(From "the book", written by lwall...)

================================================== snip
#!/usr/bin/perl
$_ = <> until ($mode,$file) = /^begin\s*(\d*)\s*(\S*)/;
open(OUT,"> $file") if $file ne "";
while (<>) {
	last if /^end/;
	next if /[a-z]/;
	next unless int((((ord() - 32) & 077) + 2) / 3) == int(length() / 4);
	print OUT unpack("u", $_);
}
chmod oct($mode), $file;
================================================== snip
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

roy@cs.umn.edu (Roy M. Silvernail) (09/03/90)

composer@chem.bu.edu (Jeff Kellem) writes:

> In article <VX6uo4w162w@cybrspc>
> 	cybrspc!roy@cs.umn.edu (Roy M. Silvernail) writes:
>  > Has anyone implemented UUdecode in Perl?
> 
> As of version 3.0 patchlevel 28 of perl, uu{de,en}coding is builtin to perl
> via the `pack' and `unpack' functions.

That'll be great when I can get up to pl28. Currently, I'm stuck at pl18
with the MS-DOS version.

I guess the next question is... has anyone built the MS-DOS port to
pl28?

Thanks for the reply, Jeff.
--
Roy M. Silvernail |+|  roy%cybrspc@cs.umn.edu  |+| #define opinions ALL_MINE;
main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
"This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me

phil@ux1.cso.uiuc.edu (09/04/90)

First of I want to get the clerical details out of the way:

> Roy M. Silvernail |+|  roy%cybrspc@cs.umn.edu  |+| #define opinions ALL_MINE;
---->                    ^^^^^^^^^^^^^^^^^^^^^^
> main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
> "This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me

) From MAILER-DAEMON@cs.umn.edu Mon Sep  3 12:48:54 1990
) Received: from cs.umn.edu by ux1.cso.uiuc.edu with SMTP id AA23861
)   (5.64+/IDA-1.3.4 for phil); Mon, 3 Sep 90 12:48:51 -0500
) Received: from ux1.cso.uiuc.edu by cs.umn.edu (5.59/1.14)
) 	id AA27209; Mon, 3 Sep 90 12:48:29 CDT
) Date: Mon, 3 Sep 90 12:48:29 CDT
) From: "Mail Delivery Subsystem" <MAILER-DAEMON@cs.umn.edu>
---->                              ^^^^^^^^^^^^^^^^^^^^^^^^
) Subject: Returned mail: Host unknown
) Message-Id: <9009031748.AA27209@cs.umn.edu>
) To: <phil@ux1.cso.uiuc.edu>
) Status: R
) 
)    ----- Transcript of session follows -----
) 550 <roy%cybrspc@cs.umn.edu>... Host unknown
---->  ^^^^^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^

And now to the original message:

)    ----- Unsent message follows -----
) Received: from ux1.cso.uiuc.edu by cs.umn.edu (5.59/1.14)
) 	id AA27201; Mon, 3 Sep 90 12:48:29 CDT
) Received: by ux1.cso.uiuc.edu id AA23837
)   (5.64+/IDA-1.3.4 for roy%cybrspc@cs.umn.edu); Mon, 3 Sep 90 12:48:33 -0500
) Date: Mon, 3 Sep 90 12:48:33 -0500
) From: Phil Howard KA9WGN <phil@ux1.cso.uiuc.edu>
) Message-Id: <9009031748.AA23837@ux1.cso.uiuc.edu>
) To: roy%cybrspc@cs.umn.edu
) 
) I guess you are one of those people I cannot just do a reply to from news
) since "cyprspc" is not known to uucp, but at least you give a signature.
) 
) > Has anyone implemented UUdecode in Perl?
) > 
) > I'm working on a Perl project (targeted at MS-DOS) that will include
) > UUdecoding files. In the interest of conserving disk overhead, I'd
) > rather not create a temp file and spawn UUdecode. I'd rather just
) > accomplish the whole project in Perl. If a Perl UUdecode already has
) > been written, it would save me re-inventing a wheel.
) > 
) > Just another Perl newbie...
) 
) While you are at it, I'd like to encourage you to include the capability
) to encode and decode the XXencode format as well.  XX is an alternative
) to UU that uses a different character set that does not run into the
) ambiguities of translation between IBM's EBCDIC character set and ASCII.
) XXencode works where UUencode does (requiring and XXdecoder of course)
) but also works in (non-UNIX) places that UU won't.
) 
) XX uses the following character set:
) 
) +-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
) 
) representing the binary values from 0 to 63 in that order.  By using
) a string index you can convert a 6 bit value to a character.  My C
) implementation built an array of 256 bytes indexed by the arriving
) character for the decoding process.  All the rest of the format is
) identical to the UUencode format.
) 
) UUencode won't go away, but XXencode is starting to see some uses
) among those in, or exchanging data with, IBM mainframes.
) 
) Various sources are available on:
)     wuarchive.wustl.edu: (various paths)
)     ux1.cso.uiuc.edu:unix/xxcp  (these are the originals)
) 

flee@guardian.cs.psu.edu (Felix Lee) (09/04/90)

Gak.  I hadn't looked at pl28 yet.

Why uuencode?  Why not btoa, compress, rad-50, snefru, and morse code?

And how about the %r roman numeral conversion for printf?
--
Felix Lee	flee@cs.psu.edu

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (09/04/90)

In article <1043800001@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu writes:
: ) While you are at it, I'd like to encourage you to include the capability
: ) to encode and decode the XXencode format as well.  XX is an alternative
: ) to UU that uses a different character set that does not run into the
: ) ambiguities of translation between IBM's EBCDIC character set and ASCII.
: ) XXencode works where UUencode does (requiring and XXdecoder of course)
: ) but also works in (non-UNIX) places that UU won't.
: ) 
: ) XX uses the following character set:
: ) 
: ) +-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
: ) 
: ) representing the binary values from 0 to 63 in that order.  By using
: ) a string index you can convert a 6 bit value to a character.  My C
: ) implementation built an array of 256 bytes indexed by the arriving
: ) character for the decoding process.  All the rest of the format is
: ) identical to the UUencode format.
: ) 
: ) UUencode won't go away, but XXencode is starting to see some uses
: ) among those in, or exchanging data with, IBM mainframes.

    tr/-+0-9A-Za-z/! "-_/;

Sheesh.

Larry