DEV Community

Cover image for Released Result::Simple - a dead simple perl-ish Result like F#, Rust, Go
kobaken
kobaken

Posted on

2

Released Result::Simple - a dead simple perl-ish Result like F#, Rust, Go

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)
}
Enter fullscreen mode Exit fullscreen mode

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!

Image description

Feel free to give it a try!

——-

This blog is for Japanese perl advent calendar 2024.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay