Sometimes …‎

… call non-existent methods on sometimes non-existent objects.

Try to fill a list like Hash content

use Try::Chain qw(try_chain);
%hash = (
    foo => scalar try_chain {
        $obj
            ->foo(1)
            ->bar(2)
            ->baz(3, 4);
    },
)

Carefully fill a list like Hash content

use Try::Chain qw($call_m);
%hash = (
    bar => scalar $obj
        ->$call_m(foo => 1)
        ->$call_m(bar => 2)
        ->$call_m(baz => 3, 4),
)

Try to fetch a mixed data structure

use Try::Chain qw(try_chain);
@more = try_chain {
    no autovivification;
    $any
        ->foo
        ->[0]
        ->bar
        ->{2}
        ->baz;
};

Carefully fetch a mixed data structure

use Try::Chain qw($call_m $fetch_ei $fetch_ek);
@more = $any
    ->$call_m('foo')
    ->$fetch_ei(0)
    ->$call_m('bar')
    ->fetch_ek(2)
    ->$call_m('baz');

$call_m in productive code

use Try::Chain qw($call_m);
my $email = $location
    ->address
    ->email({
        is_default => 1,
        is_active  => 1,
    })
    ->$call_m('first')
    ->$call_m(get_column => 'address');

$call_em in productive code

use Try::Chain qw($call_em);
my $customer_id = $order_message
    ->address
    ->$call_em('customer_id');

$fetch_ek in productive code

use Try::Chain qw($fetch_ek);
$delivery_ref
    ->{head}
    ->$fetch_ek('document')
    ->$fetch_ek('pdf');

Module

use Try::Chain;

Exports nothing by default, full import list is

use Try::Chain qw(
    try catch finally
    try_chain
    $call_m  $call_em
    $fetch_i $fetch_ei
    $fetch_k $fetch_ek
);

End

Thank you!

Module: https://metacpan.org/pod/Try::Chain

Slides: http://download.steffen-winkler.de/gpw2020/