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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.