| 1 | package Boolean::Verbose; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base qw/Exporter/; |
|---|
| 6 | use overload (map {$_ => \&_bool} qw/"" nomethod/); |
|---|
| 7 | |
|---|
| 8 | our @EXPORT = qw/false true/; |
|---|
| 9 | |
|---|
| 10 | sub _bool { return shift()->{bool} } |
|---|
| 11 | sub new { shift; bless {@_} } |
|---|
| 12 | sub true { __PACKAGE__->new( bool => 1, message => shift) } |
|---|
| 13 | sub false { __PACKAGE__->new( bool => 0, message => shift) } |
|---|
| 14 | sub message{ return shift()->{message} } |
|---|
| 15 | |
|---|
| 16 | =head1 NAME |
|---|
| 17 | |
|---|
| 18 | Boolean::Verbose - return true or false with verbose message |
|---|
| 19 | |
|---|
| 20 | =head1 VERSION |
|---|
| 21 | |
|---|
| 22 | Version 0.01 |
|---|
| 23 | |
|---|
| 24 | =cut |
|---|
| 25 | |
|---|
| 26 | our $VERSION = '0.01'; |
|---|
| 27 | |
|---|
| 28 | =head1 SYNOPSIS |
|---|
| 29 | |
|---|
| 30 | use Boolean::Verbose; |
|---|
| 31 | |
|---|
| 32 | if(my $r = check()){ |
|---|
| 33 | # OK, you can see why you are OK. |
|---|
| 34 | print $r->message; |
|---|
| 35 | }else{ |
|---|
| 36 | # NG, you can see why you are NG. |
|---|
| 37 | print $r->message; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | sub check{ |
|---|
| 41 | return false('you are not 20.') unless is_age_20(); |
|---|
| 42 | return false('you are not male') unless is_male(); |
|---|
| 43 | return true('you are 20 and male'); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | =head1 DESCRIPTION |
|---|
| 47 | |
|---|
| 48 | Boolean::Verbose returns 1 or 0 with verbose message. |
|---|
| 49 | |
|---|
| 50 | What for it is used? It is for bad code. |
|---|
| 51 | |
|---|
| 52 | for example; |
|---|
| 53 | |
|---|
| 54 | sub check{ |
|---|
| 55 | .... |
|---|
| 56 | return 0 if not is_over_18(...); |
|---|
| 57 | .... |
|---|
| 58 | return 0 if not is_male(...); |
|---|
| 59 | .... |
|---|
| 60 | return 1; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | unless(check()){ |
|---|
| 64 | print "error." |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | You maintain this code and your boss order following to you; |
|---|
| 68 | "Change behavior by the result of check function. |
|---|
| 69 | If not is_over_18, call for_child function. |
|---|
| 70 | If not is_male, call for_female function." |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | You can change return value, but this check function is used other places. |
|---|
| 74 | So, if you chenage this value, you have to change other places. It is hard and not interesting task. |
|---|
| 75 | You wouldn't change return value, but You need some additional information. |
|---|
| 76 | You can do it with Boolean::Verbose. |
|---|
| 77 | |
|---|
| 78 | use Boolean::Verbose; |
|---|
| 79 | |
|---|
| 80 | sub check{ |
|---|
| 81 | .... |
|---|
| 82 | return false("not_over_18.") if not is_over_18(...); |
|---|
| 83 | .... |
|---|
| 84 | return false("not_male.") if not is_male(...);; |
|---|
| 85 | .... |
|---|
| 86 | return true("all_ok"); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | unless(my $bool = check()){ |
|---|
| 90 | my $msg = $bool->message; |
|---|
| 91 | if($msg eq 'not_over_18'){ |
|---|
| 92 | for_child(...); |
|---|
| 93 | }elsif($msg eq 'not_male'){ |
|---|
| 94 | for_female(...); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | =head1 EXPORT |
|---|
| 99 | |
|---|
| 100 | =over |
|---|
| 101 | |
|---|
| 102 | =item true |
|---|
| 103 | |
|---|
| 104 | true('you are right'); |
|---|
| 105 | |
|---|
| 106 | It returns 1, but it is object. When you use 'message' method. |
|---|
| 107 | You can see 'you are right'. |
|---|
| 108 | |
|---|
| 109 | =item false |
|---|
| 110 | |
|---|
| 111 | true('you are wrong'); |
|---|
| 112 | |
|---|
| 113 | It returns 0, but it is object. When you use 'message' method. |
|---|
| 114 | You can see 'you are wrong'. |
|---|
| 115 | |
|---|
| 116 | =back |
|---|
| 117 | |
|---|
| 118 | =head1 METHOD |
|---|
| 119 | |
|---|
| 120 | =over 4 |
|---|
| 121 | |
|---|
| 122 | =item new |
|---|
| 123 | |
|---|
| 124 | Boolean::Verbose->new(bool => 1, message => 'message'); |
|---|
| 125 | |
|---|
| 126 | This constructor is used internaly. So, you don't need to call this. |
|---|
| 127 | |
|---|
| 128 | =item message |
|---|
| 129 | |
|---|
| 130 | $obj->message; |
|---|
| 131 | |
|---|
| 132 | You can see verbose message used as true or false argument. |
|---|
| 133 | |
|---|
| 134 | =back |
|---|
| 135 | |
|---|
| 136 | =head1 AUTHOR |
|---|
| 137 | |
|---|
| 138 | Ktat, C<< <ktat at cpan.org> >> |
|---|
| 139 | |
|---|
| 140 | =head1 BUGS |
|---|
| 141 | |
|---|
| 142 | Please report any bugs or feature requests to |
|---|
| 143 | C<bug-boolean-verbose at rt.cpan.org>, or through the web interface at |
|---|
| 144 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Boolean-Verbose>. |
|---|
| 145 | I will be notified, and then you'll automatically be notified of progress on |
|---|
| 146 | your bug as I make changes. |
|---|
| 147 | |
|---|
| 148 | =head1 SUPPORT |
|---|
| 149 | |
|---|
| 150 | You can find documentation for this module with the perldoc command. |
|---|
| 151 | |
|---|
| 152 | perldoc Boolean::Verbose |
|---|
| 153 | |
|---|
| 154 | You can also look for information at: |
|---|
| 155 | |
|---|
| 156 | =over 4 |
|---|
| 157 | |
|---|
| 158 | =item * AnnoCPAN: Annotated CPAN documentation |
|---|
| 159 | |
|---|
| 160 | L<http://annocpan.org/dist/Boolean-Verbose> |
|---|
| 161 | |
|---|
| 162 | =item * CPAN Ratings |
|---|
| 163 | |
|---|
| 164 | L<http://cpanratings.perl.org/d/Boolean-Verbose> |
|---|
| 165 | |
|---|
| 166 | =item * RT: CPAN's request tracker |
|---|
| 167 | |
|---|
| 168 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Boolean-Verbose> |
|---|
| 169 | |
|---|
| 170 | =item * Search CPAN |
|---|
| 171 | |
|---|
| 172 | L<http://search.cpan.org/dist/Boolean-Verbose> |
|---|
| 173 | |
|---|
| 174 | =back |
|---|
| 175 | |
|---|
| 176 | =head1 ACKNOWLEDGEMENTS |
|---|
| 177 | |
|---|
| 178 | =head1 COPYRIGHT & LICENSE |
|---|
| 179 | |
|---|
| 180 | Copyright 2006 Ktat, all rights reserved. |
|---|
| 181 | |
|---|
| 182 | This program is free software; you can redistribute it and/or modify it |
|---|
| 183 | under the same terms as Perl itself. |
|---|
| 184 | |
|---|
| 185 | =cut |
|---|
| 186 | |
|---|
| 187 | 1; # End of Boolean::Verbose |
|---|