X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Foverload.t;h=0798a91b9ffb8fe3a73a2cd12e7d36e58af62530;hb=70ee44090a3faf48295f65df1a1fe1c53d04be10;hp=cf49eac45a01e602e09efa06e24ebc60557a773c;hpb=6050d10e6008e3d6e86de76c85d93bf5c06336aa;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/overload.t b/lib/overload.t index cf49eac..0798a91 100644 --- a/lib/overload.t +++ b/lib/overload.t @@ -41,7 +41,7 @@ sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead package main; -$test = 0; +our $test = 0; $| = 1; print "1..",&last,"\n"; @@ -1064,7 +1064,26 @@ package main; my $utfvar = new utf8_o 200.2.1; -test("$utfvar" eq 200.2.1); # 223 +test("$utfvar" eq 200.2.1); # 223 - stringify +test("a$utfvar" eq "a".200.2.1); # 224 - overload via sv_2pv_flags + +# 225..227 -- more %{} tests. Hangs in 5.6.0, okay in later releases. +# Basically this example implements strong encapsulation: if Hderef::import() +# were to eval the overload code in the caller's namespace, the privatisation +# would be quite transparent. +package Hderef; +use overload '%{}' => sub { (caller(0))[0] eq 'Foo' ? $_[0] : die "zap" }; +package Foo; +@Foo::ISA = 'Hderef'; +sub new { bless {}, shift } +sub xet { @_ == 2 ? $_[0]->{$_[1]} : + @_ == 3 ? ($_[0]->{$_[1]} = $_[2]) : undef } +package main; +my $a = Foo->new; +$a->xet('b', 42); +print $a->xet('b') == 42 ? "ok 225\n" : "not ok 225\n"; +print defined eval { $a->{b} } ? "not ok 226\n" : "ok 226\n"; +print $@ =~ /zap/ ? "ok 227\n" : "not ok 227\n"; # Last test is: -sub last {223} +sub last {227}