home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!news.cerf.net!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Subject: Re: unpack and shift idiom?
- Message-ID: <1992Dec19.215050.23788@netlabs.com>
- Sender: news@netlabs.com
- Nntp-Posting-Host: scalpel.netlabs.com
- Organization: NetLabs, Inc.
- References: <1992Dec16.180058.15351@bcars6a8.bnr.ca> <1992Dec18.185143.17096@bcars6a8.bnr.ca>
- Date: Sat, 19 Dec 1992 21:50:50 GMT
- Lines: 44
-
- In article <1992Dec18.185143.17096@bcars6a8.bnr.ca> jsparkes@bcars68a.bnr.ca (Jeff Sparkes) writes:
- : In article <1992Dec16.180058.15351@bcars6a8.bnr.ca> jsparkes@bcars68a.bnr.ca (Jeff Sparkes) writes:
- : >I'm reading binary data from a network stream. I've accumulated the data in
- : >a variable called $data, and am taking it apart in chunks. The code
- : >looks like:
- : > @output = unpack("L4S6", $data);
- : > $data = substr($data, 28);
- : > @names = unpack("A20", $data);
- : > ???
- : >
- : >I'm concerned by the need to use a constant as the arg to substr, especially
- : >since I calculated it wrong the first time, and it took me a while to find.
- : >This gets especially hairy when I pull strings out of the data stream, since
- : >I don't know how many bytes to skip after unpacking A strings because some
- : >stripping occurs.
- : >
- : >Please tell me I'm missing something!
- :
- : Well, one thing that I'm missing is that unpack works with fixed size
- : structures, and expects strings to be fixed length. (I thought the number
- : was how many strings to read, not the legnth of the string. I should learn
- : to read documenation properly!) Anyway, I was dealing with structures like:
- : struct junk {
- : long one, two;
- : short three, four;
- : char *s1;
- : char *s2;
- : }
- : which it seems perl it not well suited for.
-
- If you really want to unpack a pointer, try the 'p' template specifier.
- But I'm curious as to how you can read a meaningful char* from a network
- stream. The situation doesn't arise often in practice.
-
- : One wish though: it would be great if unpack would actually remove the
- : bytes from the front of the string, or at least provide some way to find
- : out how many bytes were used in the unpacking.
-
- You make it sound as though the number of bytes used is indeterminate.
- The number of bytes used is unambiguously determined by the template,
- unless you use a *, in which case you either don't care anymore, or you
- intend to measure the length of what was returned by the *.
-
- Larry
-