Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!purdue!bu.edu!nntp-read!jbw From: jbw@bucsf.bu.edu (Joe Wells) Newsgroups: comp.lang.perl Subject: loops, conditional expressions, and pattern match variables Message-ID: Date: 8 Mar 90 00:43:06 GMT Sender: news@bu.edu.bu.edu Distribution: comp Organization: Boston University Computer Science Department Lines: 36 Discovered an interesting undocumented gotcha with the use of $1 and related pattern match variables, illustrated by the following short program: $_ = 'foobar'; m/^(...)(...)$/; print "A 1: $1, 2: $2\n"; while (! m/^(..)(..)$/) { print "B 1: $1, 2: $2\n"; $_ = 'fubu'; } print "C 1: $1, 2: $2\n"; while (! m/^(..)(..)$/) { print "never reached\n"; } print "D 1: $1, 2: $2\n"; which produces this output: A 1: foo, 2: bar B 1: foo, 2: bar C 1: foo, 2: bar D 1: fu, 2: bu Essentially, the first time the EXPR in a "while" statement is evaluated, any changes to the values of the pattern match variables ($1, $&, et. al.) are visible outside the while statement. The second and succeeding times the EXPR is evaluated, such changes are only visible inside the loop. Neat, huh? Sure threw me for a loop. -- Joe Wells jbw%bucsf.bu.edu@cs.bu.edu ...!harvard!bu-cs!bucsf!jbw