Try::Chain - Call method, hash and/or array chains with break on undef
0.005
The module exports:
imported from Try::Tiny
implemented here to call a complete chain or break
implemented here to call a method or break
implemented here to call an existing method or break
implemented here to fetch an array index or break
implemented here to fetch an existing array index or break
implemented here to fetch a hash key or break
implemented here to fetch an existing hash key or break
Import what needed. The following code describes the full import:
use Try::Chain qw( try catch finally try_chain $call_m $call_em $fetch_i $fetch_ei $fetch_k $fetch_ek );
Inside of this Distribution is a directory named example. Run this *.pl files.
Call method, hash and/or array chains with break on undef means, that in some cases it is ok to get back nothing late or early.
In case of method chain like
my $scalar = $obj->foo(1)->bar(2)->baz(3); my %hash = ( any => 'any', baz => scalar $obj->foo(1)->bar(2)->baz(3), );
and foo or bar can return nothing or undef, you get an error: Can't call method ... on an undefined value.
A quick solution is:
my $scalar = $obj->foo(1) && $obj->foo(1)->bar(2) && $obj->foo(1)->bar(2)->baz(3); my %hash = ( any => 'any', baz => scalar $obj->foo(1) && $obj->foo(1)->bar(2) && $obj->foo(1)->bar(2)->baz(3), );
In case of method foo and/or bar is performance critical code it is a bad idea to call the method code more then one time. The the solution looks like this:
my $foo = $obj->foo(1); my $bar = $foo && $foo->bar(2); my $scalar = $bar && $bar->baz(3); my %hash = ( any => 'any', baz => do { my $foo = $obj->foo(1); my $bar = $foo && $foo->bar(2); $bar && scalar $bar->baz(3); }, );
This module allows to call the chain by ignoring all undef errors in block:
my $scalar = try_chain { $obj->foo(1)->bar(2)->baz(3) }; my %hash = ( any => 'any', baz => scalar try_chain { $obj->foo(1)->bar(2)->baz(3) }, );
Or better step by step?
my $scalar = $obj->$call_m('foo', 1)->$call_m('bar', 2)->$call_m('baz', 3); my %hash = ( any => 'any', baz => scalar $obj ->$call_m('foo', 1) ->$call_m('bar', 2) ->$call_m('baz', 3), );
Also possible with maybe not existing hash or array references:
... = try_chain { $any->foo->[0]->bar(@params)->{key}->baz };
... = $any ->$call_m('foo') ->$fetch_i(0) ->$call_m(bar => @params) ->$fetch_k('key') ->$call_m('baz');
Full Try::Tiny support:
... = try_chain { ... } catch { ... } finally { ... };
Switch off possible autovivication:
$result = try_chain { no autovivification; $any->foo->{key}->bar(@params)->[0]->baz; }; @result = try_chain { no autovivification; $any->foo->{key}->bar(@params)->[0]->baz; };
Calls the whole try block, breaks and ignores undef errors.
Calls the next method if possible.
Calls the next method if possible and method exists.
Calls the next index of an array reference if possible.
Calls the next index of an array reference if possible and index exists.
Calls the next key of a hash reference if possible.
Calls the next key of a hash reference if possible and key exists.
none
nothing
parent
Exporter
Try::Tiny
not known
autovivification
Steffen Winkler
Copyright (c) 2017, Steffen Winkler <steffenw at cpan.org>. All rights reserved.
<steffenw at cpan.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Try::Chain, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Try::Chain
CPAN shell
perl -MCPAN -e shell install Try::Chain
For more information on module installation, please visit the detailed CPAN module installation guide.