File Coverage

lib/Scope/UndefSafe.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 29 29 100.0


line stmt bran cond sub pod time code
1             package Scope::UndefSafe;
2 3     3   32805 use strict;
  3         3  
  3         69  
3 3     3   9 use warnings;
  3         3  
  3         54  
4 3     3   15 use utf8;
  3         1  
  3         11  
5              
6             our $VERSION = "0.02";
7              
8 3     3   99 use Exporter qw/import/;
  3         3  
  3         357  
9             our @EXPORT_OK = qw/let apply/;
10              
11             sub let (&$; &) {
12 18     18 1 7336 my ($func, $value) = @_;
13              
14 18 100       42 if (defined $value) {
15 16         16 local $_ = $value;
16 16         24 return $func->();
17             }
18             }
19              
20             sub apply (&$; &) {
21 9     9 1 5585 my ($func, $value) = @_;
22 9         15 let(\&$func, $value);
23 9         1940 return $value;
24             }
25              
26             1;
27             __END__
28              
29             =encoding utf-8
30              
31             =head1 NAME
32              
33             Scope::UndefSafe - The functions to limit the scope.
34              
35             =head1 SYNOPSIS
36              
37             use Scope::UndefSafe qw/let apply/;
38              
39             my $obj = AnyObject->new;
40             let { $_->method() } $obj; # `method` is executed.
41             apply { $_->method() } $obj; # `method` is executed, and return $obj.
42              
43             $obj = undef;
44             let { $_->method() } $obj; # `method` is not executed.
45             apply { $_->method() } $obj; # `method` is not executedm, but return $obj.
46              
47              
48             =head1 DESCRIPTION
49              
50             Scope::UndefSafe has two functions to limit scope undef safety.
51              
52             =head2 METHODS
53              
54             =head3 C<let>
55              
56             Invoke block if pass non undef value as second argument.
57             And return block returned value.
58              
59             The following two are the same behavior.
60              
61             let { $_->method() } $obj;
62             $obj ? $obj->method() : undef;
63              
64             =head3 C<apply>
65              
66             Invoke block if pass non undef value as a second argument.
67             And return a second argument.
68              
69             The following two are the same behavior.
70              
71             apply { $_->method() } $obj;
72             $obj ? do { $obj->method(); $obj } : undef;
73              
74             =head1 SEE ALSO
75              
76             =over
77              
78             =item * L<stdlib.kotlin.let|https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/let.html>
79              
80             =item * L<stdlib.kotlin.apply|https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html>
81              
82             =back
83              
84             =head1 LICENSE
85              
86             The MIT License (MIT)
87              
88             Copyright (c) 2016 Pine Mizune
89              
90             Permission is hereby granted, free of charge, to any person obtaining a copy
91             of this software and associated documentation files (the "Software"), to deal
92             in the Software without restriction, including without limitation the rights
93             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
94             copies of the Software, and to permit persons to whom the Software is
95             furnished to do so, subject to the following conditions:
96              
97             The above copyright notice and this permission notice shall be included in
98             all copies or substantial portions of the Software.
99              
100             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
101             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
102             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
103             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
104             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
105             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
106             THE SOFTWARE.
107              
108             =head1 AUTHOR
109              
110             Pine Mizune E<lt>pinemz@gmail.comE<gt>
111              
112             =cut
113