3 # This is a test for bugs in (?{ }) and (??{ }) caused by corrupting the regex
4 # engine state within the eval-ed code
12 fresh_perl_is(<<'CODE', 'ok', {});
13 '42' =~ /4(?{ 'foo' =~ m{(foo)} })2/
17 fresh_perl_is(<<'CODE', 'ok', {}, 'RT#33936');
18 'aba' =~ /(??{join('',split(qr{(?=)},'aba'))})/
22 fresh_perl_is(<<'CODE', 'ok', {}, 'match vars are localized');
24 $x =~ s/(a)(?{ 'moo' =~ m{(o)} })/uc($1)/e;
25 print 'ok' if $x eq 'Aba';
28 my $preamble = <<'CODE';
30 # In the real world we would die on validation fails, but RT#27838
31 # is still unresolved, so don't tempt fate.
32 $hash->{name} =~ /^[A-Z][a-z]+ [A-Z][a-z]+$/ or return "name error";
33 $hash->{age} =~ /^[1-9][0-9]*$/ or return "age error";
35 # Add another layer of (?{...}) to try really hard to break things
37 /^(\d+)(?(?{my $sqrt = sprintf "%.0f", sqrt($^N); $sqrt**2==$^N })|(?!))$/
38 or return "squareness error";
40 return bless { %$hash }, "Foo";
46 # Do something like Regexp::Grammars does building an object.
47 my $matched = $str =~ /
49 ([A-Za-z][A-Za-z ]*)(?{ local $hash->{name} = $^N }),[ ]
50 (\d+)(?{ local $hash->{age} = $^N })[ ]years[ ]old,[ ]
51 secret[ ]number[ ](\d+)(?{ local $hash->{square} = $^N }).
52 (?{ $obj = build_obj(); })
58 print ref($obj), ":$obj->{name}:$obj->{age}:$obj->{square}";
60 print $obj, ":$hash->{name}:$hash->{age}:$hash->{square}";
63 print "no match $hash->{name}:$hash->{age}:$hash->{square}";
69 fresh_perl_is($preamble . <<'CODE', 'match Foo:John Smith:42:36', {}, 'regex distillation 1');
70 match("John Smith, 42 years old, secret number 36.");
73 fresh_perl_is($preamble . <<'CODE', 'match Foo:John Smith:42:36', {}, 'regex distillation 2');
74 match("Jim Jones, 35 years old, secret wombat 007."
75 ." John Smith, 42 years old, secret number 36.");
78 fresh_perl_is($preamble . <<'CODE', 'match squareness error:::', {}, 'regex distillation 3');
79 match("John Smith, 54 years old, secret number 7.");
82 fresh_perl_is($preamble . <<'CODE', 'no match ::', {}, 'regex distillation 4');
83 match("Jim Jones, 35 years old, secret wombat 007.");