| 1 | package Data::Result; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use boolean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | use overload | ||||||||||||||
| 12 | 2 | ▶ | fallback=>1, | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 3 | ▶ | '""'=>sub { $_[0]->msg }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 9 | ▶ | bool=>sub { $_[0]->is_true }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | ; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | our $VERSION='1.0003'; | |||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Data::Result - Handling true and false in a better way! | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | use Modern::Perl; | ||||||||||||||
| 26 | use Data::Result; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | # just true | ||||||||||||||
| 29 | my $result=Data::Result->new(is_true=>0); | ||||||||||||||
| 30 | if($result) { | ||||||||||||||
| 31 | print "Yup its true!\n"; | ||||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | # True with data | ||||||||||||||
| 35 | $result=Data::Result->new(is_true=>1,data=>'Yup This is true!'); | ||||||||||||||
| 36 | if($result) { | ||||||||||||||
| 37 | print $result->data,"\n"; | ||||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # just flase | ||||||||||||||
| 41 | $result=Data::Result->new(is_true=>0); | ||||||||||||||
| 42 | if($result) { | ||||||||||||||
| 43 | print $result->data,"\n"; | ||||||||||||||
| 44 | } else { | ||||||||||||||
| 45 | print "well, something went wrong!\n"; | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | # handle false, but give us an error! | ||||||||||||||
| 49 | $result=Data::Result->new(is_true=>0,msg=>'this is our message'); | ||||||||||||||
| 50 | if($result) { | ||||||||||||||
| 51 | print $result->data,"\n"; | ||||||||||||||
| 52 | } else { | ||||||||||||||
| 53 | print "$result\n"; | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||
| 57 | |||||||||||||||
| 58 | Handling true and false isn't always enough. This alows true and false to encapsulate things as a simple state. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =cut | ||||||||||||||
| 61 | |||||||||||||||
| 62 | # This method runs after the new constructor | ||||||||||||||
| 63 | 16 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 64 | 16 | my ($self)=@_; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | # this method runs before the new constructor, and can be used to change the arguments passed to the module | ||||||||||||||
| 68 | 17 | ▶ | around BUILDARGS => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 17 | my ($org,$class,@args)=@_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 17 | return $class->$org(@args); | |||||||||||||
| 72 | 2 | }; | |||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | =head1 Object Constructor Arguments | ||||||||||||||
| 76 | |||||||||||||||
| 77 | Data::Result provides the following constructor arguments | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Required arguments: | ||||||||||||||
| 80 | |||||||||||||||
| 81 | is_true: true or fale | ||||||||||||||
| 82 | # if not blessed it will be converted to a boolean true or false object | ||||||||||||||
| 83 | |||||||||||||||
| 84 | Optional arguments | ||||||||||||||
| 85 | |||||||||||||||
| 86 | data: Data this object contains | ||||||||||||||
| 87 | msg: A human readable string representing this object. | ||||||||||||||
| 88 | extra: another slot to put data in | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =cut | ||||||||||||||
| 91 | |||||||||||||||
| 92 | has is_true=>( | ||||||||||||||
| 93 | is=>'rw', | ||||||||||||||
| 94 | isa=>Bool, | ||||||||||||||
| 95 | required=>1, | ||||||||||||||
| 96 | 16 | ▶ | coerce=>sub { blessed $_[0] ? $_[0] : $_[0] ? true : false } | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||
| 97 | 2 | ); | |||||||||||||
| 98 | |||||||||||||||
| 99 | 2 | has msg=>( | |||||||||||||
| 100 | is=>'rw', | ||||||||||||||
| 101 | isa=>Str, | ||||||||||||||
| 102 | default=>'', | ||||||||||||||
| 103 | lazy=>1, | ||||||||||||||
| 104 | ); | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 2 | has data=>( | |||||||||||||
| 107 | is=>'rw', | ||||||||||||||
| 108 | lazy=>1, | ||||||||||||||
| 109 | ); | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 2 | has extra=>( | |||||||||||||
| 112 | is=>'rw', | ||||||||||||||
| 113 | lazy=>1, | ||||||||||||||
| 114 | ); | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =head1 OO Methods | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =over 4 | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =item * my $data=$result->get_data | ||||||||||||||
| 121 | |||||||||||||||
| 122 | Simply a wrapper for $result->data | ||||||||||||||
| 123 | |||||||||||||||
| 124 | =cut | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 0 | ▶ | sub get_data { $_[0]->data } | ||||||||||||
|
Subroutine
get_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 127 | |||||||||||||||
| 128 | =item * if($result->is_false) { ... } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | Inverts $self->is_true | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =cut | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 2 | ▶ | sub is_false { !$_[0]->is_true } | ||||||||||||
|
Subroutine
is_false: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 135 | |||||||||||||||
| 136 | =item * my $result=Data::Result->new_true($data,$extra); | ||||||||||||||
| 137 | |||||||||||||||
| 138 | Wrapper for | ||||||||||||||
| 139 | |||||||||||||||
| 140 | my $result=Data::Result->new(is_true=>1,data=>$data,extra=>$data); | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =cut | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 4 | ▶ | sub new_true { | ||||||||||||
|
Subroutine
new_true: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 145 | 4 | my ($self,$data,$extra)=@_; | |||||||||||||
| 146 | 4 | return $self->new(is_true=>1,data=>$data,extra=>$extra); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =item * my $result=Data::Result->new_false($msg,$extra); | ||||||||||||||
| 150 | |||||||||||||||
| 151 | Wrapper for | ||||||||||||||
| 152 | |||||||||||||||
| 153 | my $result=Data::Result->new(is_true=>1,msg=>$msg,extra=>$data); | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =cut | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 5 | ▶ | sub new_false { | ||||||||||||
|
Subroutine
new_false: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 158 | 5 | my ($self,$msg,$extra)=@_; | |||||||||||||
| 159 | 5 | ▶ | croak '$msg is a required argument' unless defined($msg); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 4 | return $self->new(is_true=>0,msg=>$msg,extra=>$extra); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 16 | ▶ | sub DEMOLISH { | ||||||||||||
|
Subroutine
DEMOLISH: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 164 | 16 | my ($self)=@_; | |||||||||||||
| 165 | 16 | ▶ | return unless defined($self); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 16 | delete $self->{extra}; | |||||||||||||
| 167 | 16 | delete $self->{data}; | |||||||||||||
| 168 | 16 | delete $self->{msg}; | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | =back | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =head1 See Also | ||||||||||||||
| 174 | |||||||||||||||
| 175 | L<boolean> | ||||||||||||||
| 176 | |||||||||||||||
| 177 | =head1 AUTHOR | ||||||||||||||
| 178 | |||||||||||||||
| 179 | Mike Shipper <AKALINUX@CPAN.ORG> | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =cut | ||||||||||||||
| 182 | |||||||||||||||
| 183 | 2 | 1; | |||||||||||||