1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#!./perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
}
use strict;
no warnings 'syntax';
{
# Bug #77084 points out a corruption problem when scalar //g is used
# on overloaded objects.
my @realloc;
my $TAG = "foo:bar";
use overload '""' => sub {$TAG};
my $o = bless [];
my ($one) = $o =~ /(.*)/g;
push @realloc, "xxxxxx"; # encourage realloc of SV and PVX
is $one, $TAG, "list context //g against overloaded object";
my $r = $o =~ /(.*)/g;
push @realloc, "yyyyyy"; # encourage realloc of SV and PVX
is $1, $TAG, "scalar context //g against overloaded object";
pos ($o) = 0; # Reset pos, as //g in scalar context sets it to non-0.
$o =~ /(.*)/g;
push @realloc, "zzzzzz"; # encourage realloc of SV and PVX
is $1, $TAG, "void context //g against overloaded object";
}
{
# an overloaded stringify returning itself shouldn't loop indefinitely
{
package Self;
use overload q{""} => sub {
return shift;
},
fallback => 1;
}
my $obj = bless [], 'Self';
my $r = qr/$obj/;
pass("self object, 1 arg");
$r = qr/foo$obj/;
pass("self object, 2 args");
}
{
# [perl #116823]
# when overloading regex string constants, a different code path
# was taken if the regex was compile-time, leading to overloaded
# regex constant string segments not being handled correctly.
# They were just treated as OP_CONST strings to be concatted together.
# In particular, if the overload returned a regex object, it would
# just be stringified rather than having any code blocks processed.
BEGIN {
overload::constant qr => sub {
my ($raw, $cooked, $type) = @_;
return $cooked unless defined $::CONST_QR_CLASS;
if ($type =~ /qq?/) {
return bless \$cooked, $::CONST_QR_CLASS;
} else {
return $cooked;
}
};
}
{
# returns a qr// object
package OL_QR;
use overload q{""} => sub {
my $re = shift;
return qr/(?{ $OL_QR::count++ })$$re/;
},
fallback => 1;
}
{
# returns a string
package OL_STR;
use overload q{""} => sub {
my $re = shift;
return qq/(?{ \$OL_STR::count++ })$$re/;
},
fallback => 1;
}
{
# returns chr(str)
package OL_CHR;
use overload q{""} => sub {
my $chr = shift;
return chr($$chr);
},
fallback => 1;
}
my $qr;
$::CONST_QR_CLASS = 'OL_QR';
$OL_QR::count = 0;
$qr = eval q{ qr/^foo$/; };
ok("foo" =~ $qr, "compile-time, OL_QR, single constant segment");
is($OL_QR::count, 1, "flag");
$OL_QR::count = 0;
$qr = eval q{ qr/^foo$(?{ $OL_QR::count++ })/; };
ok("foo" =~ $qr, "compile-time, OL_QR, multiple constant segments");
is($OL_QR::count, 2, "qr2 flag");
# test /foo.../ when foo is given string overloading,
# for various permutations of '...'
$::CONST_QR_CLASS = 'OL_STR';
for my $has_re_eval (0, 1) {
for my $has_qr (0, 1) {
for my $has_code (0, 1) {
for my $has_runtime (0, 1) {
for my $has_runtime_code (0, 1) {
if ($has_runtime_code) {
next unless $has_runtime;
}
note( "re_eval=$has_re_eval "
. "qr=$has_qr "
. "code=$has_code "
. "runtime=$has_runtime "
. "runtime_code=$has_runtime_code");
my $eval = '';
$eval .= q{use re 'eval'; } if $has_re_eval;
$eval .= q{$match = $str =~ };
$eval .= q{qr} if $has_qr;
$eval .= q{/^abc};
$eval .= q{(?{$blocks++})} if $has_code;
$eval .= q{$runtime} if $has_runtime;
$eval .= q{/; 1;};
my $runtime = q{def};
$runtime .= q{(?{$run_blocks++})} if $has_runtime_code;
my $blocks = 0;
my $run_blocks = 0;
my $match;
my $str = "abc";
$str .= "def" if $runtime;
my $result = eval $eval;
my $err = $@;
$result = $result ? 1 : 0;
if (!$has_re_eval) {
is($result, 0, "EVAL: $eval");
like($err, qr/Eval-group not allowed at runtime/,
"\$\@: $eval");
next;
}
is($result, 1, "EVAL: $eval");
diag("\$@=[$err]") unless $result;
is($match, 1, "MATCH: $eval");
is($blocks, $has_code, "blocks");
is($run_blocks, $has_runtime_code, "run_blocks");
}
}
}
}
}
# if the pattern gets (undetectably in advance) upgraded to utf8
# while being concatenated, it could mess up the alignment of the code
# blocks, giving rise to 'Eval-group not allowed at runtime' errs.
$::CONST_QR_CLASS = 'OL_CHR';
{
my $count = 0;
is(eval q{ "\x80\x{100}" =~ /128(?{ $count++ })256/ }, 1,
"OL_CHR eval + match");
is($count, 1, "OL_CHR count");
}
undef $::CONST_QR_CLASS;
}
{
# [perl #115004]
# array interpolation within patterns should handle qr overloading
# (like it does for scalar vars)
{
package P115004;
use overload 'qr' => sub { return qr/a/ };
}
my $o = bless [], 'P115004';
my @a = ($o);
ok("a" =~ /^$o$/, "qr overloading with scalar var interpolation");
ok("a" =~ /^@a$/, "qr overloading with array var interpolation");
}
{
# if the pattern gets silently re-parsed, ensure that any eval'ed
# code blocks get the correct lexical scope. The overloading of
# concat, along with the modification of the text of the code block,
# ensures that it has to be re-compiled.
{
package OL_MOD;
use overload
q{""} => sub { my ($pat) = @_; $pat->[0] },
q{.} => sub {
my ($a1, $a2) = @_;
$a1 = $a1->[0] if ref $a1;
$a2 = $a2->[0] if ref $a2;
my $s = "$a1$a2";
$s =~ s/x_var/y_var/;
bless [ $s ];
},
;
}
BEGIN {
overload::constant qr => sub { bless [ $_[0] ], 'OL_MOD' };
}
$::x_var = # duplicate to avoid 'only used once' warning
$::x_var = "ABC";
my $x_var = "abc";
$::y_var = # duplicate to avoid 'only used once' warning
$::y_var = "XYZ";
my $y_var = "xyz";
use re 'eval';
my $a = 'a';
ok("xyz" =~ m{^(??{ $x_var })$}, "OL_MOD");
ok("xyza" =~ m{^(??{ $x_var })$a$}, "OL_MOD runtime");
}
done_testing();
|