Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Variable scoping oddity inside (??{ ... })by Roger (Parson) |
on Oct 31, 2003 at 03:22 UTC ( [id://303483]=perlquestion: print w/replies, xml ) | Need Help?? |
Roger has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks, I saw the question Regexp for alphabetical order match within the string this morning. So I decided to give it a try, and came up with the following solution with matching time interpolation (??{ ... }) inside the regular expression - What I want to achieve in subroutine in_sequence is as follows: However I am not getting the expected result. The values returned by the subroutine for $str1 and $str2 are both true.
So I expanded the regular expression and inserted a few print statements. The debug version of the code is as follows:
It seems that something odd is happing to the variable scoping inside the match-time interpolation (??{ ... }), the variable assignment wrapped inside has no effect on my variables inside the subroutine, probably creates its own private variable, even when I do not explicitly declare them with my.When I ran the code, I got the following output: It seems that the value of my $ok has not been modified by the code $ok = 0 inside (??{ ... }). This suddenly made me curious and created the following test code to investigate the scoping of the variable $ok - And this time, the output becomes - It actually worked when I move the $ok variable up one level to the file scope! Can anyone please explain to me why, by moving my $ok up one level into file scope, made the assignment $ok = 0 inside (??{ ... }) to work all the sudden? Thanks in advance!
Back to
Seekers of Perl Wisdom
|
|