home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5511 < prev    next >
Encoding:
Text File  |  1992-08-26  |  1.9 KB  |  66 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!merlin.dev.cdx.mot.com!lezz
  3. From: lezz@merlin.dev.cdx.mot.com (Lezz Giles)
  4. Subject: Re: What's wrong with system command?
  5. Message-ID: <1992Aug26.170740.1334@merlin.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: monarch.dev.cdx.mot.com
  8. Reply-To: lezz@merlin.dev.cdx.mot.com (Lezz Giles)
  9. Organization: Motorola Codex, Canton, MA
  10. References: <l8qmm3INNoli@jethro.Corp.Sun.COM> <BtK9vp.J79@acsu.buffalo.edu>
  11. Date: Wed, 26 Aug 1992 17:07:40 GMT
  12. Lines: 52
  13.  
  14. In article <BtK9vp.J79@acsu.buffalo.edu>, yli@acsu.buffalo.edu (Yanhong Li) writes:
  15. |>I used a system command in the following perl script, to my suprise, the
  16. |>print command was not executed, if I remove the system command, print will 
  17. |>be executed, why? and how can I make the print command successful without
  18. |>removing the system command?
  19. |>
  20. |>#!/usr/local/bin/perl
  21. |>
  22. |>open(LEX, "/imagesG/yli/G02.ex" );
  23. |>
  24. |>while(<LEX>){
  25. |>
  26. |>print $_;
  27. |>
  28. |>($word1, $word2)=split;
  29. |>
  30. |>system "/users/usps/yli/stem $word2";
  31. |>
  32. |>}
  33. |>close LEX
  34.  
  35. I can't answer your question but I can suggest that you rewrite it slightly...
  36.  
  37. #!/usr/local/bin/perl
  38.  
  39. open(LEX, "/imagesG/yli/G02.ex") || die "Cannot open /imagesG/yli/G02.ex, $!";
  40.  
  41. while (<LEX>) {
  42.  
  43. print $_;      # or even just `print;`
  44.  
  45. ($word1, $word2) = split;
  46.  
  47. $word2 || die "Unsplittable line at $.\n$_";
  48.  
  49. $retval = system "/users/usps/yli/stem $word2";
  50.  
  51. if (($retval >> 8) != 0) {
  52.     die "Execution of /users/usps/yli/stem $word2 failed - $retval\n";
  53. }
  54.  
  55. }
  56.  
  57. close LEX;
  58.  
  59. (sorry about the indentation!)  One of the major joys of perl (in my mind)
  60. is that it is incredibly easy to add error-checking/handling.  In my experience
  61. it has almost always proved worth-while adding as much error-checking as
  62. possible even for short scripts - no program works first time, and comprehensive
  63. error checking really helps you narrow down problems quickly.
  64.  
  65. Lezz "GIGO" Giles
  66.