NAME
safenav - Safe-navigation for Perl
SYNOPSIS
use safenav;
my $tire_age = $car
-> safenav::wrap()
-> wheels()
-> [0] # undef, if no wheels at all.
-> tire() # undef, if no tire on the wheel.
-> {created_on}
-> delta_days($now)
-> safenav::unwrap();
unless (defined $tire_age) {
# The car either have no wheels, or the first wheel has no tire.
...
}
DESCRIPTION
This safenav
pragma provides helper methods 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.
This pragma is part of PerlX::SafeNav. It is just an alternative interface.
Say we have this chain, in which each part right after ther ->
operator may yield undef
:
$o->a()->{b}->c()->[42]->d();
To make it safe from undef
values, we mark the beginning and the end with safenav::wrap()
and safenav::unwrap()
:
$o-> safenav::wrap() -> a()->{b}->c()->[42]->d() -> safenav::unwrap();
Or alternatively, with safenav::begin()
and safenav::end()
:
$o-> safenav::begin() -> a()->{b}->c()->[42]->d() -> safenav::end();
Whichever seems better for you.