From owner-freebsd-questions Mon Nov 27 6:50:10 2000 Delivered-To: freebsd-questions@freebsd.org Received: from bcfw1d.bridge.com (bcfw1d.ext.bridge.com [167.76.159.31]) by hub.freebsd.org (Postfix) with ESMTP id 1124537B479 for ; Mon, 27 Nov 2000 06:50:07 -0800 (PST) Received: (from uucp@localhost) by bcfw1d.bridge.com (8.10.2/8.10.2) id eAREp3Y05969; Mon, 27 Nov 2000 08:51:03 -0600 (CST) Received: from unknown(167.76.56.34) by bcfw1d.bridge.com via smap (V5.5) id xma005905; Mon, 27 Nov 00 08:50:54 -0600 Received: from mnmailhost (mnmailhost.bridge.com [167.76.155.14]) by mail1srv.bridge.com (8.8.8/8.7.3) with SMTP id IAA23265; Mon, 27 Nov 2000 08:49:53 -0600 (CST) Received: from 89-7 by mnmailhost (SMI-8.6/SMI-4.1) id JAA03860; Mon, 27 Nov 2000 09:49:47 -0500 To: Subject: Re: OT Perl question References: From: Tim Ayers Date: 27 Nov 2000 08:49:44 -0600 In-Reply-To: "Craig Beasland"'s message of "Mon, 27 Nov 2000 20:41:55 +0800" Message-ID: <66l9mpvb.fsf@tim.bridge.com> Lines: 67 User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG >>>>> "C" == Craig Beasland writes: C> Hi there, C> I am trying to build a Netscape Client Kit and have 1 final problem. You C> take all the data from the user and write a perl script which passes it back C> to the Netsape program. I am not sure of how to begin with the perl script. C> Is there anyone out there who can help. C> The instructions from Netscape are as follows... C> ======= C> The binary file format used by this MIME type is simple, yet extensible. It C> consists of consecutive name/value pairs separated by 4 bytes of size C> information: C> | 4 bytes | ------ x bytes ------ | 4 bytes | ------ y bytes ------ | (x) C> (name) (y) (value) C> The parser iterates as follows until it reaches the end of the stream: C> Read 4 bytes of binary data and cast to int for size of Name in bytes C> (x) C> Read (x) bytes of ASCII data for the name C> Read 4 bytes of binary data and cast to int for size of Value in bytes C> (y) C> Read (y) bytes of ASCII data for the value C> ================ Here's a straight-forward perl script that will read this type of data stream and print out the resulting values. You might need to tweak the "L" in the unpack statement. "L" handles unsigned 32-bit integers. Do 'perldoc -f pack' for more info. #!/usr/bin/perl -w use strict; while (1) { for (1..2) { my ($len, $str); read(STDIN, $len, 4) or exit; $len = unpack("L", $len); # convert len to a number read(STDIN, $str, $len) or exit; print "$len -> $str "; } print "\n"; } And here's a testing script that will generate such a stream. #!/usr/bin/perl -w use strict; $| = 1; # autoflush STDOUT open(WORDS, ") { next unless /\S/; #Skip blank lines (just in case) chop; my $l = length; my $f = pack("La$l", $l, $_); print $f; } HTH and Hope you have a very nice day, :-) Tim Ayers (tayers@bridge.com) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message