https://metacpan.org/pod/Result::Simple
Result::Simple is a dead simple Perl-ish Result.
Result represents a function’s return value as success or failure, enabling safer error handling and more effective control flow management. This pattern is used in other languages such as F#, Rust, and Go.
In Perl, this pattern is also useful, and this module provides a simple way to use it. This module does not wrap a return value in an object. Just return a tuple like ($data, undef) or (undef, $err). For example, take a look at a implement of Ok function:
# When the function is successful, it should return this.
sub Ok {
if (CHECK_ENABLED) {
croak "`Ok` must be called in list context" unless wantarray;
croak "`Ok` does not allow multiple arguments" if @_ > 1;
croak "`Ok` does not allow no arguments" if @_ == 0;
}
($_[0], undef)
}
It is a dead simple!
However, it decreases the bugs. For instance, if you forget to handle $err, it can be detected by Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter. This is one of effectiveness of Result pattern!
Feel free to give it a try!
——-
This blog is for Japanese perl advent calendar 2024.
Source link
lol