File Coverage

lib/Value/Object.pm
Criterion Covered Total %
statement 27 27 100.0
branch 5 6 83.3
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package Value::Object;
2              
3 13     13   55310 use warnings;
  13         25  
  13         387  
4 13     13   62 use strict;
  13         23  
  13         4252  
5              
6             our $VERSION = '0.15';
7              
8             sub value
9             {
10 106     106 1 11489 my ($self) = @_;
11 106         132 return ${$self};
  106         506  
12             }
13              
14             sub new
15             {
16 130     130 1 59665 my ($class, $value) = @_;
17 130         286 my $self = bless \$value, $class;
18              
19 130         421 my ($why, $long, $data) = $self->_why_invalid( $value );
20 130 100       448 $self->_throw_exception( $why, $long, $data ) if defined $why;
21 49         170 ${$self} = $self->_untaint( $self->value );
  49         90  
22              
23 49         174 return $self;
24             }
25              
26             # A subclass must override this method or _is_invalid to be able to create value objects.
27             sub _why_invalid
28             {
29 3     3   7 my ($self, $value) = @_;
30 3 100       12 return ( ref($self) . ": Invalid parameter when creating value object.", "", undef )
31             unless $self->_is_valid( $value );
32 2         21 return;
33             }
34              
35             # A subclass must override this method or _why_invalid to be able to create value objects.
36             sub _is_valid
37             {
38 1     1   3 my ($self, $value) = @_;
39 1         6 return;
40             }
41              
42             # Default exception support just uses die to throw an exception.
43             sub _throw_exception
44             {
45 81     81   146 my ($self, $why, $longmsg, $data) = @_;
46 81         805 die $why;
47             }
48              
49             # Brute force untaint.
50             sub _untaint
51             {
52 49     49   80 my ($self, $value) = @_;
53              
54             # Can only untaint scalars
55 49 50       155 return $value if ref $value;
56              
57             # This is usually a very bad idea. It should be safe here because the class
58             # has, by definition, validated the input before we get to this function.
59             # If there is a problem, the validation code should be corrected.
60 49         145 $value =~ m/\A(.*)\z/sm;
61 49         137 return $1;
62             }
63              
64             1;
65             __END__