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

  1. Path: sparky!uunet!olivea!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!europa.asd.contel.com!awds.imsd.contel.com!news.cerf.net!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Bug with foreach/local in 4.035
  5. Message-ID: <1992Dec31.210225.26222@netlabs.com>
  6. Date: 31 Dec 92 21:02:25 GMT
  7. References: <SOUP.92Dec28105043@ampersand.ampersand.com> <1992Dec29.224047.20472@news.eng.convex.com>
  8. Sender: news@netlabs.com
  9. Distribution: comp
  10. Organization: NetLabs, Inc.
  11. Lines: 31
  12. Nntp-Posting-Host: scalpel.netlabs.com
  13.  
  14. In article <1992Dec29.224047.20472@news.eng.convex.com> tchrist@convex.COM (Tom Christiansen) writes:
  15. : From the keyboard of soup@ampersand.com (Doug Campbell):
  16. : :There appears to be a bug (or at least a surprising behavior) in 4.035
  17. : :regarding foreach and local.  It seems that a construct like:
  18. : :
  19. : :    foreach $i (@list)
  20. : :
  21. : :will  cause a subsequent (down the call stack) "local ($i)" to cause the
  22. : :item  in  @list  currently  referenced  by $i  to appear  blank.   
  23. : That's a manifestation of "variable suicide".  You can read about it 
  24. : in the FAQ.
  25.  
  26. This works right in Perl 5, because local saves the scalar pointer
  27. rather than the scalar value.  On top of this, you'll probably use
  28. "my" rather than "local", which will be immune to all the problems of
  29. dynamic scoping.
  30.  
  31. An interesting question is whether the implicit dynamic scoping of the loop
  32. variable should change to lexical scoping.  I don't think it should, for
  33. two reasons.  It would be unclear how far the scope of the variable extends,
  34. and it would break some existing scripts.
  35.  
  36. Hmm.  The way things work, it might be possible to force lexical scoping
  37. with
  38.  
  39.     for my $i (1..10)
  40.  
  41. Hmm.  Hafta think about that...
  42.  
  43. Larry
  44.