Just another Perl shrine PerlMonks

### 'my' inside /(?{...})/ doesn't refresh a hash variable in 5.14

by rsFalse (Friar)
 on Nov 11, 2018 at 16:33 UTC Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I was playing with '/(?{ ... })/' and have found different 'my' behaviour inside eval-block, in 5.18 vs 5.14.
In the code below, I am playing with the task: "To find 2 numbers (from 0 to 2), which can not be the same, their sum is equal to 2, and the second one is bigger than first one".
```    use warnings;
use strict;
\$\ = \$/;
my \$A = 2;

my \$_0to2 = join '', 0 .. 2;
\$_ = join '=', ( \$_0to2 ) x 2;

print "\\$_:[\$_]";

print /^
\d*(\d)\d* = \d*(\d)\d*
(?(?{ my %h; map \$h{ \$_ } ++, \$1, \$2; 2 == keys %h }) | (*
+FAIL) )
(?(?{ \$1 < \$2 }) | (*FAIL) )
(?(?{ my \$sum = \$1 + \$2; \$sum == \$A }) | (*FAIL) )
\$/x ?
"MATCHED: \\$1:[\$1], \\$2:[\$2]"
:
"NOT_MATCHED"
;
Here I used conditionals "(?(condition)yes-pattern|no-pattern)" (perlre).

5.14.4 OUTPUT:
```\$_:[012=012]
NOT_MATCHED
5.18.2 OUTPUT:
```\$_:[012=012]
MATCHED: \$1:[0], \$2:[2]
I haven't understand where is the different behaviour, because I haven't received any warnings by 'use warnings;'.
I found the difference when inserted 'print 0 + keys %h;' inside eval-block with hash (code line below). It printed many '3' (5.14) vs not more than '2' (5.18). It seems that 'my %h;' haven't refreshed a hash. I guess it was known behaviour. When have it changed?
Upd.:
```(?(?{ my %h; map \$h{ \$_ } ++, \$1, \$2; print 0 + keys %h; 2 == keys %h
+}) | (*FAIL) )
Upd.2:Have swapped versions to correct

Replies are listed 'Best First'.
Re: 'my' inside /(?{...})/ doesn't refresh a hash variable in 5.14
by LanX (Archbishop) on Nov 11, 2018 at 16:57 UTC
I can't check at the moment, but its most likely a bug which was fixed.

If you need a workaround , put the code into a anonymous sub.

Cheers Rolf
(addicted to the Perl gramming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1225584]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2019-06-19 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Is there a future for codeless software?

Results (83 votes). Check out past polls.

Notices?