develooper Front page | perl.perl5.porters | Postings from November 1999

Regex match variables, threads, and recursion

From:
Dan Sugalski
Date:
November 5, 1999 09:52
Subject:
Regex match variables, threads, and recursion
Message ID:
3.0.6.32.19991105125447.00c0f840@tuatha.sidhe.org
A couple of days ago someone (mjd?) was wondering if the problem that
threads have with the regex match variables (where the regex variables are
tied to pieces of the code and reflect the last thread that executed it)
occurred with non-threaded code too. Just for chuckles, I decided to try it
this afternoon and, as expected, the problem can be duplicated with a
non-threaded perl build. Witness:

	$search_string = "123abc";

	sub foo {
	    $again = shift;
	    $search_string =~ /(\d+)(\w+)/;
	    print "$1 $2\n";
	    return unless $again;
	    $search_string = "456def";
	    foo(0);
	    print "$1 $2\n";
	}

	foo(1);

produces:

123 abc
456 def
456 def

I don't recall seeing this documented anywhere, though it very well might be.

Given that this is going to be fixed in one way or another for threaded
perl, do we want to go all the way and fix it so non-threaded perl does
lexical scoping for the match variables too?

					Dan

----------------------------------------"it's like this"-------------------
Dan Sugalski                            even samurai
dan@sidhe.org                           have teddy bears and even
                                        teddy bears get drunk



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About