From 5649f766a3f28cf9368305072b3351733d675e56 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 7 Jul 2011 20:21:34 -0500 Subject: make the error line up with the one used in core --- lib/smartmatch/engine/core.pm | 8 ++++---- t/error.t | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 t/error.t diff --git a/lib/smartmatch/engine/core.pm b/lib/smartmatch/engine/core.pm index ee3c81e..410c983 100644 --- a/lib/smartmatch/engine/core.pm +++ b/lib/smartmatch/engine/core.pm @@ -4,6 +4,7 @@ use warnings; use 5.010; use B; +use Carp qw(croak); use Hash::Util::FieldHash qw(idhash); use Scalar::Util qw(blessed looks_like_number reftype); use overload (); @@ -62,12 +63,11 @@ sub match { # see http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2011-07/msg00214.html if (!$overload && overload::Overloaded($b)) { $overload = overload::Method($a, '~~'); - die "no ~~ overloading on $b" - unless $overload; - return $a->$overload($b, 0); + return $a->$overload($b, 0) + if $overload; } - die "no ~~ overloading on $b" + croak("Smart matching a non-overloaded object breaks encapsulation") unless $overload; return $b->$overload($a, 1); } diff --git a/t/error.t b/t/error.t new file mode 100644 index 0000000..10d69be --- /dev/null +++ b/t/error.t @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +my $foo = bless {}; +my $bar = bless {}; + +eval '$foo ~~ $bar'; +my $core_error = $@; +$core_error =~ s/\d+/XXX/g; +(my $short_core_error = $core_error) =~ s/ at .* line .*//; + +{ + use smartmatch 'core'; + eval '$foo ~~ $bar'; + my $engine_error = $@; + $engine_error =~ s/\d+/XXX/g; + (my $short_engine_error = $engine_error) =~ s/ at .* line .*//; + is($short_engine_error, $short_core_error); + { local $TODO = "Carp is dumb"; + is($engine_error, $core_error); + } +} + +done_testing; -- cgit v1.2.3