Skip site navigation (1)Skip section navigation (2)
Date:      27 Nov 2000 08:49:44 -0600
From:      Tim Ayers <tayers@bridge.com>
To:        <questions@FreeBSD.ORG>
Subject:   Re: OT Perl question
Message-ID:  <66l9mpvb.fsf@tim.bridge.com>
In-Reply-To: "Craig Beasland"'s message of "Mon, 27 Nov 2000 20:41:55 %2B0800"
References:  <B1471D5DCC74D4119444004005E23A2001CF0E@CORONA>

next in thread | previous in thread | raw e-mail | index | archive | help
>>>>> "C" == Craig Beasland <craig@hotmix.com.au> 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, "</usr/share/dict/words") or die "Can't open 'dict/words': $!";
  while (<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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?66l9mpvb.fsf>