line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package safenav; |
2
|
1
|
|
|
1
|
|
238737
|
use strict; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
28
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
447
|
use PerlX::SafeNav ('$safenav', '$unsafenav', '&safenav'); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
157
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
7
|
use Exporter 'import'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
90
|
|
7
|
|
|
|
|
|
|
our @EXPORT = ('&safenav'); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
*begin = *wrap = $safenav; |
10
|
|
|
|
|
|
|
*end = *unwrap = $unsafenav; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
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 pragma is part of L. It provides alternative interfaces for wrapping a chain of calls and make it safe from encountering C values in the way. If any of sub-expressions yield C, instead of aborting the program with an error message, the entire chain yields C 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: |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$o->a()->{b}->c()->[42]->d(); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
To make it safe from encountering C values, we wrap the chain with C and C: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$o-> safenav::wrap() -> a()->{b}->c()->[42]->d() -> safenav::unwrap(); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
... or with C and C: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$o-> safenav::begin() -> a()->{b}->c()->[42]->d() -> safenav::end() |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
... or, with a C 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, L |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |