home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7626 < prev    next >
Encoding:
Internet Message Format  |  1992-12-29  |  1.5 KB

  1. Path: sparky!uunet!noc.near.net!ampersand.com!ftp.ampersand.com!soup
  2. From: soup@ampersand.com (Doug Campbell)
  3. Newsgroups: comp.lang.perl
  4. Subject: Bug with foreach/local in 4.035
  5. Date: 28 Dec 92 10:50:43
  6. Organization: Ampersand Inc., Westford, Mass.
  7. Lines: 50
  8. Distribution: comp
  9. Message-ID: <SOUP.92Dec28105043@ampersand.ampersand.com>
  10. NNTP-Posting-Host: ampersand.ampersand.com
  11.  
  12.  
  13. There appears to be a bug (or at least a surprising behavior) in 4.035
  14. regarding foreach and local.  It seems that a construct like:
  15.  
  16.     foreach $i (@list)
  17.  
  18. will  cause a subsequent (down the call stack) "local ($i)" to cause the
  19. item  in  @list  currently  referenced  by $i  to appear  blank.   For
  20. example, this program:
  21.  
  22. -------------------------------
  23. sub fn
  24. {
  25.     local ($i);
  26.     print ("nums= @nums\n");
  27. };
  28.  
  29. @nums = (1, 2, 3);
  30. print ("nums= @nums\n");
  31.  
  32. foreach $i (@nums)
  33. {
  34.     &fn ($i);
  35. };
  36. -------------------------------
  37. prints out:
  38.  
  39. nums= 1 2 3
  40. nums=  2 3
  41. nums= 1  3
  42. nums= 1 2 
  43.  
  44. whereas it should print out:
  45.  
  46. nums= 1 2 3
  47. nums= 1 2 3
  48. nums= 1 2 3
  49. nums= 1 2 3
  50.  
  51. Changing  the  "local  ($i)"  to "local  ($j)" fixes  the problem, but
  52. having to make  sure there  are no variable  name conflicts is hard to
  53. do.   Instead,  my current workaround  is to  change  the  "foreach $i
  54. (@nums)" line to "foreach $i (@unused = @nums)", and  never use vector
  55. @unused.  A little ugly, but workable.
  56.  
  57. These  results were obtained with perl  4.035 on  SunOS 4.1.1 compiled
  58. with Sun's cc.
  59.  
  60.                         Doug Campbell
  61.                         soup@ampersand.com
  62.