home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5505 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  2.4 KB

  1. Path: sparky!uunet!olivea!decwrl!sdd.hp.com!usc!rpi!uwm.edu!linac!att!news.cs.indiana.edu!nstn.ns.ca!dragon.acadiau.ca!merlin!peter
  2. From: peter@merlin.acadiau.ca (Peter Steele)
  3. Newsgroups: comp.lang.perl
  4. Subject: Simultaneous processes
  5. Message-ID: <root.714838821@merlin>
  6. Date: 26 Aug 92 14:20:21 GMT
  7. Sender: news@dragon.acadiau.ca
  8. Organization: Acadia University
  9. Lines: 76
  10. Nntp-Posting-Host: merlin
  11.  
  12. How does one set up simultaneous processes in perl? For example,
  13. how would I translate the following into perl:
  14.  
  15.     #!/bin/sh
  16.  
  17.     ls -la > d1 &
  18.     ls -la > d2 &
  19.     ls -la > d3 &
  20.     wait
  21.     cat d1 d2 d3 > d4
  22.  
  23. This is a pretty simplistic example, but illustrates what I want to
  24. do in perl. The real program looks like this:
  25.  
  26.     #!/lbin/perl
  27.  
  28.     @dlist1 = split(/,/,$ARGV[0]);
  29.     @dlist2 = split(/,/,$ARGV[1]);
  30.  
  31.     for ($i = 0; $i <= $#dlist1; $i++) {
  32.         for ($j = -1; $j <= $#dlist2; $j++) {
  33.             foreach $d (@dlist1[0..$i]) {
  34.                 spawn a process for $d and save its output somehow &
  35.             }
  36.             foreach $d (@dlist2[0..$j]) {
  37.                 spawn a process for $d and save its output somehow &
  38.             }
  39.             wait for all processes to complete and combine output;
  40.         }
  41.     }
  42.  
  43. This program takes two comma-separated lists and loops over the
  44. values provided. If the program is called "fred", it would be
  45. called as, for example,
  46.  
  47.     fred d1,d2 e1,e2,e3
  48.  
  49. The loops generate sets of values in the following pattern:
  50.  
  51.     d1
  52.     d1,e1
  53.     d1,e1,e2
  54.     d1,e1,e2,e3
  55.     d1,d2
  56.     d1,d2,e1
  57.     d1,d2,e1,e2
  58.     d1,d2,e1,e2,e3
  59.  
  60. I want to spawn off simulataneous processes like this:
  61.  
  62.     do something with d1 &
  63.     wait
  64.     collect combined output
  65.  
  66.     do something with d1 &
  67.     do something with e1 &
  68.     wait
  69.     collect combined output
  70.  
  71. and so on, with the last interation generating
  72.  
  73.     do something with d1 &
  74.     do something with d2 &
  75.     do something with e1 &
  76.     do something with e2 &
  77.     do something with e3 &
  78.     wait
  79.     collect combined output
  80.  
  81. Conceptually straightforward, but I'm not sure how to accomplish this
  82. in perl. I'd also be happy with a pure Bourne shell solution. In fact,
  83. that would probably be preferable. Does anyone know how to translate
  84. the for loops above into shell?
  85. --
  86. Peter Steele        Unix Services Manager            peter.steele@acadiau.ca 
  87. Acadia Univ., Wolfville, NS, Canada B0P 1X0  902-542-2201  Fax: 902-542-4364
  88.