+# ref tests
+
+my %res;
+
+for my $str ("x", "\x{B6}") {
+ utf8::upgrade($str) if $str !~ /x/;
+ for my $chr (qw/S A H G X ( * F/) {
+ for my $op (qw/| & ^/) {
+ my $co = ord $chr;
+ my $so = ord $str;
+ $res{"$chr$op$str"} = eval qq/chr($co $op $so)/;
+ }
+ }
+ $res{"undef|$str"} = $str;
+ $res{"undef&$str"} = "";
+ $res{"undef^$str"} = $str;
+}
+
+sub PVBM () { "X" }
+1 if index "foo", PVBM;
+
+my $warn = 0;
+local $^W = 1;
+local $SIG{__WARN__} = sub { $warn++ };
+
+sub is_first {
+ my ($got, $orig, $op, $str, $name) = @_;
+ is(substr($got, 0, 1), $res{"$orig$op$str"}, $name);
+}
+
+for (
+ # [object to test, first char of stringification, name]
+ [undef, "undef", "undef" ],
+ [\1, "S", "scalar ref" ],
+ [[], "A", "array ref" ],
+ [{}, "H", "hash ref" ],
+ [qr/x/, "(", "qr//" ],
+ [*foo, "*", "glob" ],
+ [\*foo, "G", "glob ref" ],
+ [PVBM, "X", "PVBM" ],
+ [\PVBM, "S", "PVBM ref" ],
+ [bless([], "Foo"), "F", "object" ],
+) {
+ my ($val, $orig, $type) = @$_;
+
+ for (["x", "string"], ["\x{B6}", "utf8"]) {
+ my ($str, $desc) = @$_;
+ utf8::upgrade($str) if $desc =~ /utf8/;
+
+ $warn = 0;
+
+ is_first($val | $str, $orig, "|", $str, "$type | $desc");
+ is_first($val & $str, $orig, "&", $str, "$type & $desc");
+ is_first($val ^ $str, $orig, "^", $str, "$type ^ $desc");
+
+ is_first($str | $val, $orig, "|", $str, "$desc | $type");
+ is_first($str & $val, $orig, "&", $str, "$desc & $type");
+ is_first($str ^ $val, $orig, "^", $str, "$desc ^ $type");
+
+ my $new;
+ ($new = $val) |= $str;
+ is_first($new, $orig, "|", $str, "$type |= $desc");
+ ($new = $val) &= $str;
+ is_first($new, $orig, "&", $str, "$type &= $desc");
+ ($new = $val) ^= $str;
+ is_first($new, $orig, "^", $str, "$type ^= $desc");
+
+ ($new = $str) |= $val;
+ is_first($new, $orig, "|", $str, "$desc |= $type");
+ ($new = $str) &= $val;
+ is_first($new, $orig, "&", $str, "$desc &= $type");
+ ($new = $str) ^= $val;
+ is_first($new, $orig, "^", $str, "$desc ^= $type");
+
+ if ($orig eq "undef") {
+ # undef |= and undef ^= don't warn
+ is($warn, 10, "no duplicate warnings");
+ }
+ else {
+ is($warn, 0, "no warnings");
+ }
+ }
+}
+
+delete $SIG{__WARN__};
+
+my $strval;
+
+{
+ package Bar;
+ use overload q/""/ => sub { $strval };
+
+ package Baz;
+ use overload q/|/ => sub { "y" };
+}
+
+ok(!eval { 1 if bless([], "Bar") | "x"; 1 },"string overload can't use |");
+like($@, qr/no method found/, "correct error");
+is(eval { bless([], "Baz") | "x" }, "y", "| overload works");
+
+my $obj = bless [], "Bar";
+$strval = "x";
+eval { $obj |= "Q" };
+$strval = "z";
+is("$obj", "z", "|= doesn't break string overload");
+
+# [perl #29070]
+$^A .= new version ~$_ for eval sprintf('"\\x%02x"', 0xff - ord("1")),
+ $::IS_EBCDIC ? v13 : v205, # 255 - ord('2')
+ eval sprintf('"\\x%02x"', 0xff - ord("3"));
+is $^A, "123", '~v0 clears vstring magic on retval';
+
+{
+ my $w = $Config::Config{ivsize} * 8;
+
+ fail("unexpected w $w") unless $w == 32 || $w == 64;
+
+ is(1 << 1, 2, "UV 1 left shift 1");
+ is(1 >> 1, 0, "UV 1 right shift 1");
+
+ is(0x7b << -4, 0x007, "UV left negative shift == right shift");
+ is(0x7b >> -4, 0x7b0, "UV right negative shift == left shift");
+
+ is(0x7b << 0, 0x07b, "UV left zero shift == identity");
+ is(0x7b >> 0, 0x07b, "UV right zero shift == identity");
+
+ is(0x0 << -1, 0x0, "zero left negative shift == zero");
+ is(0x0 >> -1, 0x0, "zero right negative shift == zero");
+
+ cmp_ok(1 << $w - 1, '==', 2 ** ($w - 1), # not is() because NV stringify.
+ "UV left $w - 1 shift == 2 ** ($w - 1)");
+ is(1 << $w, 0, "UV left shift $w == zero");
+ is(1 << $w + 1, 0, "UV left shift $w + 1 == zero");
+
+ is(1 >> $w - 1, 0, "UV right shift $w - 1 == zero");
+ is(1 >> $w, 0, "UV right shift $w == zero");
+ is(1 >> $w + 1, 0, "UV right shift $w + 1 == zero");
+
+ # Negative shiftees get promoted to UVs before shifting. This is
+ # not necessarily the ideal behavior, but that is what is happening.
+ if ($w == 64) {
+ no warnings "portable";
+ no warnings "overflow"; # prevent compile-time warning for ivsize=4
+ is(-1 << 1, 0xFFFF_FFFF_FFFF_FFFE,
+ "neg UV (sic) left shift = 0xFF..E");
+ is(-1 >> 1, 0x7FFF_FFFF_FFFF_FFFF,
+ "neg UV (sic) right right = 0x7F..F");
+ } elsif ($w == 32) {
+ no warnings "portable";
+ is(-1 << 1, 0xFFFF_FFFE, "neg left shift == 0xFF..E");
+ is(-1 >> 1, 0x7FFF_FFFF, "neg right right == 0x7F..F");
+ }
+
+ {
+ # 'use integer' means use IVs instead of UVs.
+ use integer;
+
+ # No surprises here.
+ is(1 << 1, 2, "IV 1 left shift 1 == 2");
+ is(1 >> 1, 0, "IV 1 right shift 1 == 0");
+
+ # The left overshift should behave like without 'use integer',
+ # that is, return zero.
+ is(1 << $w, 0, "IV 1 left shift $w == 0");
+ is(1 << $w + 1, 0, "IV 1 left shift $w + 1 == 0");
+ is(-1 << $w, 0, "IV -1 left shift $w == 0");
+ is(-1 << $w + 1, 0, "IV -1 left shift $w + 1 == 0");
+
+ # Even for negative IVs, left shift is multiplication.
+ # But right shift should display the stuckiness to -1.
+ is(-1 << 1, -2, "IV -1 left shift 1 == -2");
+ is(-1 >> 1, -1, "IV -1 right shift 1 == -1");
+
+ # As for UVs, negative shifting means the reverse shift.
+ is(-1 << -1, -1, "IV -1 left shift -1 == -1");
+ is(-1 >> -1, -2, "IV -1 right shift -1 == -2");
+
+ # Test also at and around wordsize, expect stuckiness to -1.
+ is(-1 >> $w - 1, -1, "IV -1 right shift $w - 1 == -1");
+ is(-1 >> $w, -1, "IV -1 right shift $w == -1");
+ is(-1 >> $w + 1, -1, "IV -1 right shift $w + 1 == -1");
+ }
+}
+
+# [perl #129287] UTF8 & was not providing a trailing null byte.
+# This test is a bit convoluted, as we want to make sure that the string
+# allocated for &’s target contains memory initialised to something other
+# than a null byte. Uninitialised memory does not make for a reliable
+# test. So we do &. on a longer non-utf8 string first.
+for (["aaa","aaa"],[substr ("a\x{100}",0,1), "a"]) {
+ use feature "bitwise";
+ no warnings "experimental::bitwise", "pack";
+ $byte = substr unpack("P2", pack "P", $$_[0] &. $$_[1]), -1;
+}
+is $byte, "\0", "utf8 &. appends null byte";
+
+# only visible under sanitize
+fresh_perl_is('$x = "UUUUUUUV"; $y = "xxxxxxx"; $x |= $y; print $x',
+ ( $::IS_EBCDIC) ? 'XXXXXXXV' : '}}}}}}}V',
+ {}, "[perl #129995] access to freed memory");
+
+
+#
+# Using code points above 0xFF is fatal