X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Foverload.t;h=4db647dbeea3f677a5da750ad47bad380c653fd1;hb=1a95e36d92295cabb6c213a2f397c4cb7614d12c;hp=f98f0c46c0446494cceadc7b7e580cbdee6dd96b;hpb=3adf792ce57734350849757edcf4e5c2f5bb850a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/overload.t b/lib/overload.t index f98f0c4..4db647d 100644 --- a/lib/overload.t +++ b/lib/overload.t @@ -729,7 +729,7 @@ else { $iter = iterator->new(5); test scalar <${iter}>, '5'; # 176 $acc = ''; - $acc .= " $out" while $out = <${iter}>; + $acc .= " $out" while $out = <$iter>; test $acc, ' 4 3 2 1 0'; # 177 } { @@ -1046,5 +1046,43 @@ $r = Foo->new(0); test(($r || 0) == 0); # 222 +package utf8_o; + +use overload + '""' => sub { return $_[0]->{var}; } + ; + +sub new + { + my $class = shift; + my $self = {}; + $self->{var} = shift; + bless $self,$class; + } + +package main; + + +my $utfvar = new utf8_o 200.2.1; +test("$utfvar" eq 200.2.1); # 223 + +# 224..226 -- 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 224\n" : "not ok 224\n"; +print defined eval { $a->{b} } ? "not ok 225\n" : "ok 225\n"; +print $@ =~ /zap/ ? "ok 226\n" : "not ok 226\n"; + # Last test is: -sub last {222} +sub last {226}