Use Result::Simple for tuple-based error handling in Perl
Adds Result::Simple tuple-based error handling for Perl. Use when writing functions that return `($value, $error)` pairs instead of throwing exceptions, or when you need lightweight error handling without exception objects.
/plugin marketplace add kfly8/claude-cpan-plugins/plugin install cpan-result-simple@kfly8-claude-cpan-pluginsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Result::Simple provides lightweight error handling using tuples ($value, $error) instead of objects.
ok($value) / err($error)use Result::Simple qw(ok err);
sub divide {
my ($a, $b) = @_;
return err('Division by zero') if $b == 0;
return ok($a / $b);
}
my ($result, $error) = divide(10, 2);
if ($error) {
warn "Error: $error";
} else {
print "Result: $result";
}
sub process_config {
my $filename = shift;
my ($content, $read_error) = read_file($filename);
return (undef, $read_error) if $read_error;
my $data = eval { decode_json($content) };
return err("JSON error: $@") if $@;
return ok($data);
}
use Result::Simple qw(ok err result_for);
use Types::Standard qw(Dict Str);
use Types::Common::Numeric qw(PositiveInt);
use kura Error => Dict[message => Str];
# result_for requires structured error type (not plain string)
result_for calculate => PositiveInt, Error;
sub calculate {
my ($a, $b) = @_;
# Error must be structured (HashRef)
return err({ message => 'Invalid input' }) if $a < 0;
return ok($a * $b);
}
# Set RESULT_SIMPLE_CHECK_ENABLED=1 to activate type checking
# $ENV{RESULT_SIMPLE_CHECK_ENABLED} = 1;
my ($result, $error) = calculate(5, 3);
if ($error) {
warn "Error: $error->{message}";
} else {
print "Result: $result"; # 15
}
# Type checking works when RESULT_SIMPLE_CHECK_ENABLED=1
# calculate(5.5, 3); # Dies: Invalid success result (Float instead of PositiveInt)
Important: result_for requires:
Dict[...] with kura)RESULT_SIMPLE_CHECK_ENABLED=1 for runtime checking