| 1 | package Scope::UndefSafe; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | our $VERSION = "0.02"; | ||
| 7 | |||
| 8 | use Exporter qw/import/; | ||
| 9 | our @EXPORT_OK = qw/let apply/; | ||
| 10 | |||
| 11 | sub let (&$; &) { | ||
| 12 | my ($func, $value) = @_; | ||
| 13 | |||
| 14 | if (defined $value) { | ||
| 15 | local $_ = $value; | ||
| 16 | return $func->(); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | sub apply (&$; &) { | ||
| 21 | my ($func, $value) = @_; | ||
| 22 | let(\&$func, $value); | ||
| 23 | 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 |