use Test::More 'no_plan'; use Test::Exception; #### my $result; lives_ok {$result = $o->answer} 'answer worked'; is $result, 42, 'answer returned 42'; #### is live{$o->answer}, 42, 'answer worked'; #### live Returns the result of the given expression if no exception is thrown. If an exception is thrown an object is returned that evaluates to false when used in any standard boolean test or comparison operation ("<", "<=", ">", ">=", "==", "!=", "lt", "le", "gt", "ge", "eq" and "ne"). This can be used to simplify testing for exceptions. For example: foreach my $n (3,2,1,0) { is live{$n/$n}, 1, "$n/$n == 1"; }; will produce ok 1 - 3/3 == 1 ok 2 - 2/2 == 1 ok 3 - 1/1 == 1 not ok 4 - 0/0 == 1 # Failed test (test.pl at line 37) # got: 'DIED: Illegal division by zero at test.pl line 37.' # expected: '1' NOTE: that it is an object not a false value that is returned when an exception occurred. # this will pass even if foo dies ok(refaddr live {$o->foo}, 'foo returns object'; # this will pass even if foo dies unlike live {$o->foo}, '/fribble/', 'no fribble'; Appropriate care must be taken. #### use Sub::Uplevel; sub _exception_as_string { my $exception = shift; my $class = ref($exception); $exception = "$class ($exception)" if $class && "$exception" !~ m/^\Q$class/; chomp($exception); return($exception); }; { package Test::Exception::_NULL; use overload q{""} => sub { "${$_[0]}" }, map { $_ => sub { return } } ( qw( < <= > >= == != lt le gt ge eq ne bool ) ), fallback => 1; sub new { bless \$_[1], $_[0] }; }; sub live (&) { my $coderef = shift; my $value = eval { uplevel 2, $coderef }; $value = Test::Exception::_NULL->new( "DIED: ". _exception_as_string($@) ) if $@; $value; };