root/lang/perl/Boolean-Verbose/trunk/lib/Boolean/Verbose.pm @ 14193

Revision 14193, 3.6 kB (checked in by ktat, 6 years ago)

initial import

Line 
1package Boolean::Verbose;
2
3use strict;
4use warnings;
5use base qw/Exporter/;
6use overload (map {$_ => \&_bool} qw/"" nomethod/);
7
8our @EXPORT = qw/false true/;
9
10sub _bool  { return shift()->{bool} }
11sub new    { shift; bless {@_} }
12sub true   { __PACKAGE__->new( bool => 1, message => shift) }
13sub false  { __PACKAGE__->new( bool => 0, message => shift) }
14sub message{ return shift()->{message} }
15
16=head1 NAME
17
18Boolean::Verbose - return true or false with verbose message
19
20=head1 VERSION
21
22Version 0.01
23
24=cut
25
26our $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
48Boolean::Verbose returns 1 or 0 with verbose message.
49
50What for it is used? It is for bad code.
51
52for 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
67You maintain this code and your boss order following to you;
68"Change behavior by the result of check function.
69If not is_over_18, call for_child function.
70If not is_male, call for_female function."
71
72
73You can change return value, but this check function is used other places.
74So, if you chenage this value, you have to change other places. It is hard and not interesting task.
75You wouldn't change return value, but You need some additional information.
76You 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
106It returns 1, but it is object. When you use 'message' method.
107You can see 'you are right'.
108
109=item false
110
111 true('you are wrong');
112
113It returns 0, but it is object. When you use 'message' method.
114You 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
126This constructor is used internaly. So, you don't need to call this.
127
128=item message
129
130 $obj->message;
131
132You can see verbose message used as true or false argument.
133
134=back
135
136=head1 AUTHOR
137
138Ktat, C<< <ktat at cpan.org> >>
139
140=head1 BUGS
141
142Please report any bugs or feature requests to
143C<bug-boolean-verbose at rt.cpan.org>, or through the web interface at
144L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Boolean-Verbose>.
145I will be notified, and then you'll automatically be notified of progress on
146your bug as I make changes.
147
148=head1 SUPPORT
149
150You can find documentation for this module with the perldoc command.
151
152    perldoc Boolean::Verbose
153
154You can also look for information at:
155
156=over 4
157
158=item * AnnoCPAN: Annotated CPAN documentation
159
160L<http://annocpan.org/dist/Boolean-Verbose>
161
162=item * CPAN Ratings
163
164L<http://cpanratings.perl.org/d/Boolean-Verbose>
165
166=item * RT: CPAN's request tracker
167
168L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Boolean-Verbose>
169
170=item * Search CPAN
171
172L<http://search.cpan.org/dist/Boolean-Verbose>
173
174=back
175
176=head1 ACKNOWLEDGEMENTS
177
178=head1 COPYRIGHT & LICENSE
179
180Copyright 2006 Ktat, all rights reserved.
181
182This program is free software; you can redistribute it and/or modify it
183under the same terms as Perl itself.
184
185=cut
186
1871; # End of Boolean::Verbose
Note: See TracBrowser for help on using the browser.