home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!ampersand.com!ftp.ampersand.com!soup
- From: soup@ampersand.com (Doug Campbell)
- Newsgroups: comp.lang.perl
- Subject: Bug with foreach/local in 4.035
- Date: 28 Dec 92 10:50:43
- Organization: Ampersand Inc., Westford, Mass.
- Lines: 50
- Distribution: comp
- Message-ID: <SOUP.92Dec28105043@ampersand.ampersand.com>
- NNTP-Posting-Host: ampersand.ampersand.com
-
-
- There appears to be a bug (or at least a surprising behavior) in 4.035
- regarding foreach and local. It seems that a construct like:
-
- foreach $i (@list)
-
- will cause a subsequent (down the call stack) "local ($i)" to cause the
- item in @list currently referenced by $i to appear blank. For
- example, this program:
-
- -------------------------------
- sub fn
- {
- local ($i);
- print ("nums= @nums\n");
- };
-
- @nums = (1, 2, 3);
- print ("nums= @nums\n");
-
- foreach $i (@nums)
- {
- &fn ($i);
- };
- -------------------------------
- prints out:
-
- nums= 1 2 3
- nums= 2 3
- nums= 1 3
- nums= 1 2
-
- whereas it should print out:
-
- nums= 1 2 3
- nums= 1 2 3
- nums= 1 2 3
- nums= 1 2 3
-
- Changing the "local ($i)" to "local ($j)" fixes the problem, but
- having to make sure there are no variable name conflicts is hard to
- do. Instead, my current workaround is to change the "foreach $i
- (@nums)" line to "foreach $i (@unused = @nums)", and never use vector
- @unused. A little ugly, but workable.
-
- These results were obtained with perl 4.035 on SunOS 4.1.1 compiled
- with Sun's cc.
-
- Doug Campbell
- soup@ampersand.com
-