home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5325 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  2.3 KB

  1. Path: sparky!uunet!sequent!ogicse!uwm.edu!rpi!usc!elroy.jpl.nasa.gov!ames!sun-barr!news2me.ebay.sun.com!jethro.Corp.Sun.COM!eric.arnold@Sun.COM
  2. From: eric.arnold@Sun.COM (Eric arnold)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Arrays of filehandles
  5. Message-ID: <l8o27cINNocq@jethro.Corp.Sun.COM>
  6. Date: 14 Aug 92 19:26:36 GMT
  7. Article-I.D.: jethro.l8o27cINNocq
  8. References: <1992Aug13.164605.10760@unixg.ubc.ca>
  9. Reply-To: eric.arnold@Sun.COM
  10. Distribution: world
  11. Organization: Sun Microsystems, Inc.
  12. Lines: 83
  13. NNTP-Posting-Host: animus.corp.sun.com
  14.  
  15. In article 10760@unixg.ubc.ca, andy@orchid.bdc.ubc.ca (Andrew Dwelly) writes:
  16. >In article <1992Aug13.145758.14449@news.uit.no> tomg@stud.cs.uit.no (Tom  
  17. >Grydeland) writes:
  18. >
  19. >
  20. >#!/local/bin/perl
  21. >
  22. >open($a[0],"<one");
  23. >while (<$a[0]>)
  24. >{
  25. >    print;
  26. >}
  27. >
  28. >Which fails to work.
  29. >whereas, in perl 4.035 under SunOS 4.1.2, the best I've been able to
  30. >come up with is ....
  31. >#!/local/bin/perl
  32. >
  33. >open(scalar($a[0]),"<one");
  34. >$foobar = $a[0];
  35. >while (<$foobar>)
  36. >{
  37. >    print;
  38. >}
  39. >
  40.  
  41. This works, without using "scalar" (I added assignment to "@a" because
  42. it made me feel better, though I assume the above script also implies 
  43. setting "@a"):
  44.     
  45.     $fh_gen = "FH000";
  46.     for ( 1..25 ){ push( @a, $fh_gen++ ) ; }
  47.     open($a[0],"<one");
  48.     $foobar = $a[0];
  49.     while (<$foobar>)
  50.     {
  51.         print;
  52.     }
  53.  
  54. but I am also puzzled about why this doesn't work:
  55.  
  56.     $fh_gen = "FH000";
  57.     for ( 1..25 ){ push( @a, $fh_gen++ ) ; }
  58.     open($a[0],"<one");
  59.     while (<$a[0]>)
  60.     {
  61.         print;
  62.     }
  63.  
  64.  
  65.  
  66. >That is, not only must the array be made explicitly scalar during the  
  67. >assignment of the file handle, but also it must be assigned to
  68. >an ordinary scalar before use in angle brackets (I suspect this is because 
  69. >perl defines <scalar($a[0])> as a syntactic no-no).
  70.  
  71. "scalar($a[0])" should be the same as "$a[0]", no?  Isn't "$a[0]" already
  72. a scalar?  It would seem that the thing that is operating here is only
  73. that "<$foobar>" works, and "<$a[0]>" doesn't, meaning that you can't
  74. use an array element inside "<>".
  75.  
  76. >
  77. >The second version works, but is rather inelegant. Is there any way it can  
  78. >be improved to remove the superfluous $foobar ?
  79. >
  80. >Andy Dwelly (andy@bcu.ubc.ca)
  81.  
  82.  
  83. This doesn't work either:
  84.  
  85.  
  86.     $a{ "first" } = "FH";
  87.     open($a{first},"<one");
  88.     while (<$a{first}>)
  89.     {
  90.       print;
  91.     }
  92.  
  93. though if you add the intervening "$foobar", it does work.
  94.  
  95. -Eric
  96.  
  97. P.S. I tried this under perl4.019, SunOS4.1.1.
  98.