Data::Monad::Either - The Either monad
use Data::Monad::Either qw/left right/; sub get_key { my ($key) = @_; return sub { my ($data) = @_; return left('value is not a hash') unless ref($data) eq 'HASH'; return exists($data->{$key}) ? right($data->{$key}) : left("data has no values for key:$key"); }; } my $commit_data = { commit => { author => 'Larry' } }; my $right = right($data)->flat_map(get_key('commit'))->flat_map(get_key('author')); $right->value; # => 'Larry' my $not_hash = right(['Larry'])->flat_map(get_key('commit'))->flat_map(get_key('author')); $not_hash->value; # => 'value is not a hash' my $not_exists_key = right($data)->flat_map(get_key('parent_commit'))->flat_map(get_key('author')); $not_exists_key->value; # => 'data has no values for key:parent_commit'
Data::Monad::Either represents values with 2 possibilities.
The constructors of this class.
Checks if $either is right (correct) or left (failure)
$either
Overrides methods of Data::Monad::Base::Monad
Returns a list of values which is contained by $either
Run the first function if left, or the second.
These given functions take a value contained by $either.
Returns this Either monad if it is right, or returns the given value.
Returns the values contains by this, or returns the given default value.
Returns the values contains by this, or runs the given default function on the left value.
Flip the left right values.
Run the given function on the left value.
aereal <aereal@aereal.org>
Data::Monad::Base::Monad
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::Monad, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Monad
CPAN shell
perl -MCPAN -e shell install Data::Monad
For more information on module installation, please visit the detailed CPAN module installation guide.