13 # Bug #77084 points out a corruption problem when scalar //g is used
14 # on overloaded objects.
18 use overload '""' => sub {$TAG};
21 my ($one) = $o =~ /(.*)/g;
22 push @realloc, "xxxxxx"; # encourage realloc of SV and PVX
23 is $one, $TAG, "list context //g against overloaded object";
26 my $r = $o =~ /(.*)/g;
27 push @realloc, "yyyyyy"; # encourage realloc of SV and PVX
28 is $1, $TAG, "scalar context //g against overloaded object";
29 pos ($o) = 0; # Reset pos, as //g in scalar context sets it to non-0.
32 push @realloc, "zzzzzz"; # encourage realloc of SV and PVX
33 is $1, $TAG, "void context //g against overloaded object";
37 # an overloaded stringify returning itself shouldn't loop indefinitely
42 use overload q{""} => sub {
48 my $obj = bless [], 'Self';
50 pass("self object, 1 arg");
52 pass("self object, 2 args");
57 # when overloading regex string constants, a different code path
58 # was taken if the regex was compile-time, leading to overloaded
59 # regex constant string segments not being handled correctly.
60 # They were just treated as OP_CONST strings to be concatted together.
61 # In particular, if the overload returned a regex object, it would
62 # just be stringified rather than having any code blocks processed.
65 overload::constant qr => sub {
66 my ($raw, $cooked, $type) = @_;
67 return $cooked unless defined $::CONST_QR_CLASS;
69 return bless \$cooked, $::CONST_QR_CLASS;
77 # returns a qr// object
80 use overload q{""} => sub {
82 return qr/(?{ $OL_QR::count++ })$$re/;
92 use overload q{""} => sub {
94 return qq/(?{ \$OL_STR::count++ })$$re/;
104 use overload q{""} => sub {
115 $::CONST_QR_CLASS = 'OL_QR';
118 $qr = eval q{ qr/^foo$/; };
119 ok("foo" =~ $qr, "compile-time, OL_QR, single constant segment");
120 is($OL_QR::count, 1, "flag");
123 $qr = eval q{ qr/^foo$(?{ $OL_QR::count++ })/; };
124 ok("foo" =~ $qr, "compile-time, OL_QR, multiple constant segments");
125 is($OL_QR::count, 2, "qr2 flag");
128 # test /foo.../ when foo is given string overloading,
129 # for various permutations of '...'
131 $::CONST_QR_CLASS = 'OL_STR';
133 for my $has_re_eval (0, 1) {
134 for my $has_qr (0, 1) {
135 for my $has_code (0, 1) {
136 for my $has_runtime (0, 1) {
137 for my $has_runtime_code (0, 1) {
138 if ($has_runtime_code) {
139 next unless $has_runtime;
141 note( "re_eval=$has_re_eval "
144 . "runtime=$has_runtime "
145 . "runtime_code=$has_runtime_code");
147 $eval .= q{use re 'eval'; } if $has_re_eval;
148 $eval .= q{$match = $str =~ };
149 $eval .= q{qr} if $has_qr;
151 $eval .= q{(?{$blocks++})} if $has_code;
152 $eval .= q{$runtime} if $has_runtime;
155 my $runtime = q{def};
156 $runtime .= q{(?{$run_blocks++})} if $has_runtime_code;
162 $str .= "def" if $runtime;
164 my $result = eval $eval;
166 $result = $result ? 1 : 0;
169 is($result, 0, "EVAL: $eval");
170 like($err, qr/Eval-group not allowed at runtime/,
175 is($result, 1, "EVAL: $eval");
176 diag("\$@=[$err]") unless $result;
178 is($match, 1, "MATCH: $eval");
179 is($blocks, $has_code, "blocks");
180 is($run_blocks, $has_runtime_code, "run_blocks");
188 # if the pattern gets (undetectably in advance) upgraded to utf8
189 # while being concatenated, it could mess up the alignment of the code
190 # blocks, giving rise to 'Eval-group not allowed at runtime' errs.
192 $::CONST_QR_CLASS = 'OL_CHR';
196 is(eval q{ "\x80\x{100}" =~ /128(?{ $count++ })256/ }, 1,
197 "OL_CHR eval + match");
198 is($count, 1, "OL_CHR count");
201 undef $::CONST_QR_CLASS;
207 # array interpolation within patterns should handle qr overloading
208 # (like it does for scalar vars)
212 use overload 'qr' => sub { return qr/a/ };
215 my $o = bless [], 'P115004';
218 ok("a" =~ /^$o$/, "qr overloading with scalar var interpolation");
219 ok("a" =~ /^@a$/, "qr overloading with array var interpolation");
225 # if the pattern gets silently re-parsed, ensure that any eval'ed
226 # code blocks get the correct lexical scope. The overloading of
227 # concat, along with the modification of the text of the code block,
228 # ensures that it has to be re-compiled.
233 q{""} => sub { my ($pat) = @_; $pat->[0] },
236 $a1 = $a1->[0] if ref $a1;
237 $a2 = $a2->[0] if ref $a2;
239 $s =~ s/x_var/y_var/;
247 overload::constant qr => sub { bless [ $_[0] ], 'OL_MOD' };
250 $::x_var = # duplicate to avoid 'only used once' warning
254 $::y_var = # duplicate to avoid 'only used once' warning
260 ok("xyz" =~ m{^(??{ $x_var })$}, "OL_MOD");
261 ok("xyza" =~ m{^(??{ $x_var })$a$}, "OL_MOD runtime");