tokuhirom's Blog

overload behavior on Perl 5.14.2

use strict;
use warnings;
use utf8;
use 5.010000;

{
    package Foo;
    sub new { bless +{}, shift }
}

my $x = Foo->new();
say($x);

{
    eval q!
    package Foo;
    use overload q{""} => sub { "STRINGIFY" };
    !;
    die $@ if $@;
}

say($x);
my $y = Foo->new();
say($x);
say($y);

And results are

Foo=HASH(0x1d8ab78)
Foo=HASH(0x1d8ab78)
Foo=HASH(0x1d8ab78)
STRINGIFY

overloaded operators are applied to values create after overload only?

By this bug, JSON::XS have a amazing behavior.

www4071uf% cat hoge.pl
use strict;
use warnings;
use utf8;
use 5.010000;

use JSON::XS;
use JSON ();

warn JSON::XS::true;
warn JSON::true;
www4071uf% perl hoge.pl
1 at hoge.pl line 9.
1 at hoge.pl line 10.
www4071uf% cat foo.pl  
use strict;
use warnings;
use utf8;
use 5.010000;

use JSON ();
use JSON::XS;

warn JSON::XS::true;
warn JSON::true;
www4071uf% perl foo.pl 
true at foo.pl line 9.
true at foo.pl line 10.