File Coverage

blib/lib/Return/Value.pm
Criterion Covered Total %
statement 63 63 100.0
branch 28 28 100.0
condition 3 3 100.0
subroutine 30 30 100.0
pod 6 6 100.0
total 130 130 100.0


line stmt bran cond sub pod time code
1 4     4   825183 use strict;
  4         11  
  4         133  
2 4     4   20 use warnings;
  4         6  
  4         325  
3             package Return::Value;
4             {
5             $Return::Value::VERSION = '1.666004';
6             }
7             # ABSTRACT: (deprecated) polymorphic return values
8             # vi:et:sw=4 ts=4
9              
10 4     4   29 use Exporter 5.57 'import';
  4         322  
  4         132  
11 4     4   21 use Carp ();
  4         8  
  4         2120  
12              
13             our @EXPORT = qw[success failure];
14              
15              
16             # This hack probably impacts performance more than I'd like to know, but it's
17             # needed to have a hashref object that can deref into a different hash.
18             # _ah($self,$key, [$value) sets or returns the value for the given key on the
19             # $self blessed-ref
20              
21             sub _ah {
22 95     95   151 my ($self, $key, $value) = @_;
23 95         134 my $class = ref $self;
24 95         247 bless $self => "ain't::overloaded";
25 95 100       211 $self->{$key} = $value if @_ > 2;
26 95         138 my $return = $self->{$key};
27 95         148 bless $self => $class;
28 95         507 return $return;
29             }
30              
31             sub _builder {
32 23     23   68 my %args = (type => shift);
33 23 100       78 $args{string} = shift if (@_ % 2);
34 23         89 %args = (%args, @_);
35              
36 23 100       75 $args{string} = $args{type} unless defined $args{string};
37              
38 23 100       90 $args{errno} = ($args{type} eq 'success' ? undef : 1)
    100          
39             unless defined $args{errno};
40              
41 23         87 __PACKAGE__->new(%args);
42             }
43              
44              
45 10     10 1 1354 sub success { _builder('success', @_) }
46              
47              
48 13     13 1 2553 sub failure { _builder('failure', @_) }
49              
50              
51             sub new {
52 25     25 1 43 my $class = shift;
53 25         196 bless { type => 'failure', string => q{}, prop => {}, @_ } => $class;
54             }
55              
56              
57 30 100   30 1 1173 sub bool { _ah($_[0],'type') eq 'success' ? 1 : 0 }
58              
59             sub type {
60 4     4 1 8 my ($self, $value) = @_;
61 4 100       15 return _ah($self, 'type') unless @_ > 1;
62 3 100 100     153 Carp::croak "invalid result type: $value"
63             unless $value eq 'success' or $value eq 'failure';
64 2         20 return _ah($self, 'type', $value);
65             };
66              
67             foreach my $name ( qw[errno string data] ) {
68             ## no critic (ProhibitNoStrict)
69 4     4   27 no strict 'refs';
  4         7  
  4         2922  
70             *{$name} = sub {
71 40     40   74 my ($self, $value) = @_;
72 40 100       125 return _ah($self, $name) unless @_ > 1;
73 2         4 return _ah($self, $name, $value);
74             };
75             }
76              
77             sub prop {
78 5     5 1 10 my ($self, $name, $value) = @_;
79 5 100       13 return _ah($self, 'prop') unless $name;
80 2 100       7 return _ah($self, 'prop')->{$name} unless @_ > 2;
81 1         3 return _ah($self, 'prop')->{$name} = $value;
82             }
83              
84              
85             use overload
86 1     1   4 '""' => sub { shift->string },
87 13     13   4949 'bool' => sub { shift->bool },
88 11     11   1553 '==' => sub { shift->bool == shift },
89 1     1   79 '!=' => sub { shift->bool != shift },
90 1     1   3 '>' => sub { shift->bool > shift },
91 1     1   4 '<' => sub { shift->bool < shift },
92 11     11   119 'eq' => sub { shift->string eq shift },
93 2     2   470 'ne' => sub { shift->string ne shift },
94 2     2   459 'gt' => sub { shift->string gt shift },
95 2     2   463 'lt' => sub { shift->string lt shift },
96 1     1   8 '++' => sub { _ah(shift,'type','success') },
97 1     1   384 '--' => sub { _ah(shift,'type','failure') },
98 3 100   3   398 '${}' => sub { my $data = _ah($_[0],'data'); $data ? \$data : \undef },
  3         40  
99 3 100   3   11 '%{}' => sub { ref _ah($_[0],'data') eq 'HASH' ? _ah($_[0],'data') : {} },
100 4 100   4   19 '@{}' => sub { ref _ah($_[0],'data') eq 'ARRAY' ? _ah($_[0],'data') : [] },
101 4     4   9161 fallback => 1;
  4         6061  
  4         111  
102              
103              
104             "This return value is true.";
105              
106             __END__