home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6907 < prev    next >
Encoding:
Text File  |  1992-11-09  |  1.3 KB  |  44 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!sgiblab!sgigate!sgi!wdl1!wdl39!mab
  3. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  4. Subject: Re: Bug or feature ?
  5. Message-ID: <1992Nov9.184724.4240@wdl.loral.com>
  6. Sender: news@wdl.loral.com
  7. Organization: Loral Western Development Labs
  8. References: <CONS.92Nov9103421@mercury.cern.ch>
  9. Distribution: comp
  10. Date: Mon, 9 Nov 1992 18:47:24 GMT
  11. Lines: 31
  12.  
  13. In article <CONS.92Nov9103421@mercury.cern.ch> cons@mercury.cern.ch (Lionel Cons) writes:
  14. >I want to read the first two bytes of STDIN and then exec another
  15. >process that uses the remaining chars from STDIN. According to the man
  16. >pages the following script should work (if STDIN does not have the
  17. >close-on-exec flag set):
  18. >#!/usr/local/bin/perl
  19. >$c1 = getc(STDIN);
  20. >$c2 = getc(STDIN);
  21. >print "First chars: '$c1' and '$c2'\n";
  22. >exec('/bin/wc', '-c');
  23. >It does not work (wc reply is 0), why ?
  24.  
  25. BUFFERING
  26.  
  27. getc uses stdio, so when you did the first getc, stdio slurped in at least
  28. the whole first line into the buffer maybe more.  You are going to have to use
  29. sysread to do what you want. E.G.
  30.  
  31. #!/usr/local/perl
  32. sysread(STDIN,$c1,1);
  33. sysread(STDIN,$c2,1);
  34. print "First chars: '$c1' and '$c2'\n";
  35. exec('/bin/wc', '-c');
  36.  
  37. --
  38. Perl's Maternal Uncle
  39. Mark Biggar
  40. mab@wdl1.wdl.loral.com
  41.  
  42.  
  43.  
  44.