$self->{body} = sub {
my $instance = shift;
my $proxy = $instance->$accessor();
- ( defined $proxy )
- || $self->throw_error(
- "Cannot delegate $handle_name to $method_to_call because "
- . "the value of "
- . $self->associated_attribute->name
- . " is not defined",
- method_name => $method_to_call,
- object => $instance
- );
- ( blessed $proxy )
- || $self->throw_error(
- "Cannot delegate $handle_name to $method_to_call because "
- . "the value of "
- . $self->associated_attribute->name
- . " is not an object (got '$proxy')",
- method_name => $method_to_call,
- object => $instance
+
+ my $error
+ = !defined $proxy ? ' is not defined'
+ : !blessed $proxy ? qq{ is not an object (got '$proxy')}
+ : undef;
+
+ if ($error) {
+ $self->throw_error(
+ "Cannot delegate $handle_name to $method_to_call because "
+ . "the value of "
+ . $self->associated_attribute->name
+ . $error,
+ method_name => $method_to_call,
+ object => $instance
);
+ }
+
$proxy->$method_to_call(@_);
};
}
use strict;
use warnings;
-use Test::More tests => 89;
+use Test::More tests => 90;
use Test::Exception;
# Make sure that a useful error message is thrown when the delegation target is
# not an object
{
- my $i = Bar->new(foo => []);
- throws_ok { $i->foo_bar } qr/is not an object \(got 'ARRAY/,
+ my $i = Bar->new(foo => undef);
+ throws_ok { $i->foo_bar } qr/is not defined/,
+ 'useful error from unblessed reference';
+
+ my $j = Bar->new(foo => []);
+ throws_ok { $j->foo_bar } qr/is not an object \(got 'ARRAY/,
'useful error from unblessed reference';
}