Last active
October 17, 2019 21:16
-
-
Save j1n3l0/8fb2a2ea22ef44e7892d122e186f6817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Point; | |
use Data::Dumper::Concise qw< Dumper >; | |
use Scalar::Util qw< refaddr >; | |
my %x; | |
my %y; | |
use overload '""' => sub { | |
my $self = shift; | |
Dumper +{ | |
x => $x{refaddr $self}, | |
y => $y{refaddr $self}, | |
}; | |
}; | |
sub new { | |
my ($class, $args) = @_; | |
my $self = bless \do{my $anon_scalar}, $class; | |
$x{refaddr $self} = $args->{x} || 0; | |
$y{refaddr $self} = $args->{y} || 0; | |
return $self; | |
} | |
sub DESTROY { | |
my $self = shift; | |
delete $x{refaddr $self}; | |
delete $y{refaddr $self}; | |
} | |
1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test2::V0 -target => 'Point'; | |
use Data::Dumper::Concise qw< Dumper >; | |
subtest $CLASS => sub { | |
subtest 'string overloading' => sub { | |
is( | |
sprintf( '%s', $CLASS->new ), | |
Dumper({ x => 0, y => 0 }), | |
'should be a Data::Dumper representation', | |
); | |
}; | |
}; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment