NAME

safenav - Safe-navigation for Perl

SYNOPSIS

use safenav;

my $obj = Foo->new;
my $ret;

# block syntax
$ret = safenav { $_->x()->y()->z() } $obj;

# wrap + unwrap
$ret = $obj-> safenav::wrap() ->x()->y()->z() -> safenav::unwrap();

# begin + end
$ret = $obj-> safenav::begin() ->x()->y()->z() -> safenav::end();

unless (defined $ret) {
    # The car either have no wheels, or the first wheel has no tire.
    ...
}

DESCRIPTION

The safenav pragma is part of PerlX::SafeNav. It provides alternative interfaces for wrapping a chain of calls and make it safe from encountering undef values in the way. If any of sub-expressions yield undef, instead of aborting the program with an error message, the entire chain yields undef instead.

Say we have this call chain on object $o, and each sub-expression right next to the -> operators may yield undef:

$o->a()->{b}->c()->[42]->d();

To make it safe from encountering undef values, we wrap the chain with safenav::wrap() and safenav::unwrap():

$o-> safenav::wrap() -> a()->{b}->c()->[42]->d() -> safenav::unwrap();

... or with safenav::begin() and safenav::end():

$o-> safenav::begin() -> a()->{b}->c()->[42]->d() -> safenav::end()

... or, with a safenav { ... } block:

safenav {
    $_->a()->{b}->c()->[42]->d()
} $o;

... in which, $_ is the safenav-wrapped version of $o, and the chain is automaticly un-wrapped at the end.

Whichever seems better for you.

SEE ALSO

PerlX::SafeNav, results::wrap