Open p5pRT opened 8 years ago
Hi\,
based on perlre:
These special variables\, like the %+ hash and the numbered match variables ($1 \, $2 \, $3 \, etc.) are dynamically scoped until the end of the enclosing block or until the next successful match\, whichever comes first.
the following code parse a string which has the form {block1}* text* {block2} where block may be empty.
I use named capture (b1\, b2) to capture the blocks.
for each test case the following is display
test case
block1 text block2 text text
The problem can already be seen within the first iterations where $+{b1} should be undefined but has kept the previous value
adding an extra block fixes the problem
for (qw( ? {a} a{}? ?a{} ?{a} {a}?{b} {a}{b}{c} {a}{b}{c}{d} {a}bc{d} {a}bcd {?} { } {{} {a}{{{{{{b} {a}{{{{{{b {a}{{{{{{ {a}}}}}}{b} {a}}}}}}{b {a}}}}}}b} )) { #{{{ # perl bug fix print " $_\n" ; my $args = $_ ;
$args =~ s/^ (?'b1' (?:\{[^\{\}]*\}) ) //x ; print '>' . ($+{b1} // '') . "\n" ;
$args = reverse $args ;
$args =~ s/^ (?'b2' (?:\}[^\{\}]*\{) ) //x ; print '>' . reverse($+{b2} // '') . "\n" ;
my $t = reverse $args; print ">$t\n" ;
print "-----------------\n\n" ; #}}} }
On Fri\, Sep 30\, 2016 at 05:51:27PM -0700\, nadim khemir wrote:
based on perlre:
These special variables\, like the %+ hash and the numbered match variables ($1 \, $2 \, $3 \, etc.) are dynamically scoped until the end of the enclosing block or until the next successful match\, whichever comes first.
the following code parse a string which has the form {block1}* text* {block2} where block may be empty.
I use named capture (b1\, b2) to capture the blocks.
for each test case the following is display
test case
block1 text block2 text text
The problem can already be seen within the first iterations where $+{b1} should be undefined but has kept the previous value
This is intended behaviour. From the point of view of the dynamic scope of pattern match captures\, iterating a loop isn't regarded as exiting a scope and starting a new scope; rather it is seen as restarting the current scope.
A similar effect can be seen with this simplified program:
for (qw(ab XY)) { print "\nPRE: [$1][$+{foo}]\n"; /(.)(?'foo'.)/ or die; print "POST: [$1][$+{foo}]\n"; }
which outputs:
PRE: [][] POST: [a][b]
PRE: [a][b] POST: [X][Y]
Adding an extra scope like
for (qw(ab XY)) { { print "\nPRE: [$1][$+{foo}]\n"; /(.)(?'foo'.)/ or die; print "POST: [$1][$+{foo}]\n"; } }
will cause the output to change to:
PRE: [][] POST: [a][b]
PRE: [][] POST: [X][Y]
-- Fire extinguisher (n) a device for holding open fire doors.
The RT System itself - Status changed from 'new' to 'open'
A hint in the documentation would be nice :)
maybe with this example
On Mon\, Oct 3\, 2016 at 12:09 PM\, Dave Mitchell via RT \< perlbug-followup@perl.org> wrote:
On Fri\, Sep 30\, 2016 at 05:51:27PM -0700\, nadim khemir wrote:
based on perlre:
These special variables\, like the %+ hash and the numbered match variables ($1 \, $2 \, $3 \, etc.) are dynamically scoped until the end of the enclosing block or until the next successful match\, whichever comes first.
the following code parse a string which has the form {block1}* text* {block2} where block may be empty.
I use named capture (b1\, b2) to capture the blocks.
for each test case the following is display
test case
block1 text block2 text text
The problem can already be seen within the first iterations where $+{b1} should be undefined but has kept the previous value
This is intended behaviour. From the point of view of the dynamic scope of pattern match captures\, iterating a loop isn't regarded as exiting a scope and starting a new scope; rather it is seen as restarting the current scope.
A similar effect can be seen with this simplified program:
for \(qw\(ab XY\)\) \{ print "\\nPRE​: \[$1\]\[$\+\{foo\}\]\\n"; /\(\.\)\(?'foo'\.\)/ or die; print "POST​: \[$1\]\[$\+\{foo\}\]\\n"; \}
which outputs:
PRE​: \[\]\[\] POST​: \[a\]\[b\] PRE​: \[a\]\[b\] POST​: \[X\]\[Y\]
Adding an extra scope like
for \(qw\(ab XY\)\) \{ \{ print "\\nPRE​: \[$1\]\[$\+\{foo\}\]\\n"; /\(\.\)\(?'foo'\.\)/ or die; print "POST​: \[$1\]\[$\+\{foo\}\]\\n"; \} \}
will cause the output to change to:
PRE​: \[\]\[\] POST​: \[a\]\[b\] PRE​: \[\]\[\] POST​: \[X\]\[Y\]
-- Fire extinguisher (n) a device for holding open fire doors.
Migrated from rt.perl.org#129771 (status was 'open')
Searchable as RT129771$