home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7554 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.4 KB  |  57 lines

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