safenav.pm

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 19 / 19 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PerlX/SafeNav.pm ↑ Summary
1 package safenav;
2 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PerlX::SafeNav ('$safenav', '$unsafenav', '&safenav');
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our @EXPORT = ('&safenav');
8  
9 1 *begin = *wrap = $safenav;
10 1 *end = *unwrap = $unsafenav;
11  
12 1 1;
13  
14 =pod
15  
16 =encoding utf-8
17  
18 =head1 NAME
19  
20 safenav - Safe-navigation for Perl
21  
22 =head1 SYNOPSIS
23  
24 use safenav;
25  
26 my $obj = Foo->new;
27 my $ret;
28  
29 # block syntax
30 $ret = safenav { $_->x()->y()->z() } $obj;
31  
32 # wrap + unwrap
33 $ret = $obj-> safenav::wrap() ->x()->y()->z() -> safenav::unwrap();
34  
35 # begin + end
36 $ret = $obj-> safenav::begin() ->x()->y()->z() -> safenav::end();
37  
38 unless (defined $ret) {
39 # The car either have no wheels, or the first wheel has no tire.
40 ...
41 }
42  
43 =head1 DESCRIPTION
44  
45 The C<safenav> pragma is part of L<PerlX::SafeNav>. It provides alternative interfaces for wrapping a chain of calls and make it safe from encountering C<undef> values in the way. If any of sub-expressions yield C<undef>, instead of aborting the program with an error message, the entire chain yields C<undef> instead.
46  
47 Say we have this call chain on object C<$o>, and each sub-expression right next to the C<< -> >> operators may yield C<undef>:
48  
49 $o->a()->{b}->c()->[42]->d();
50  
51 To make it safe from encountering C<undef> values, we wrap the chain with C<safenav::wrap()> and C<safenav::unwrap()>:
52  
53 $o-> safenav::wrap() -> a()->{b}->c()->[42]->d() -> safenav::unwrap();
54  
55 ... or with C<safenav::begin()> and C<safenav::end()>:
56  
57 $o-> safenav::begin() -> a()->{b}->c()->[42]->d() -> safenav::end()
58  
59 ... or, with a C<safenav { ... }> block:
60  
61 safenav {
62 $_->a()->{b}->c()->[42]->d()
63 } $o;
64  
65 ... in which, C<$_> is the safenav-wrapped version of C<$o>, and the chain is automaticly un-wrapped at the end.
66  
67 Whichever seems better for you.
68  
69 =head1 SEE ALSO
70  
71 L<PerlX::SafeNav>, L<results::wrap>
72  
73 =cut
« PerlX/SafeNav.pm ↑ Summary