results
view release on metacpan or search on metacpan
lib/results/wrap.pm view on Meta::CPAN
use 5.014;
use strict;
use warnings;
use results ();
package results::wrap;
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '0.006';
BEGIN {
if ( $] ge '5.034' ) {
require experimental;
'experimental'->import( 'try' );
}
else {
require Syntax::Keyword::Try;
'Syntax::Keyword::Try'->import;
}
};
sub AUTOLOAD {
my ( $invocant, @args ) = @_;
my ( $method ) = ( our $AUTOLOAD =~ /::(\w+)$/ );
try {
return results::ok( $invocant->$method( @args ) );
}
catch ( $e ) {
return results::err( $e );
}
}
sub results::wrap (&) {
if ( @_ == 1 and ref($_[0]) eq 'CODE' ) {
my ( $code ) = @_;
try {
return results::ok( $code->() );
}
catch ( $e ) {
return results::err( $e );
}
}
else {
my ( $invocant, $method, @args ) = @_;
try {
return results::ok( $invocant->$method( @args ) );
}
catch ( $e ) {
return results::err( $e );
}
}
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
results::wrap - wrap a method call in a Result
=head1 SYNOPSIS
Assuming that C<< $object->foo( @args ) >> is a method call which might return
a value or throw an exception, you can convert it to an ok/err Result using:
use results::wrap;
( run in 3.736 seconds using v1.01-cache-2.11-cpan-5e09290becf )