File Coverage

blib/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 12     12   57147 use warnings;
  12         23  
  12         358  
4 12     12   61 use strict;
  12         24  
  12         4226  
5              
6             our $VERSION = '0.14';
7              
8             sub value
9             {
10 106     106 1 10399 my ($self) = @_;
11 106         143 return ${$self};
  106         529  
12             }
13              
14             sub new
15             {
16 130     130 1 51566 my ($class, $value) = @_;
17 130         293 my $self = bless \$value, $class;
18              
19 130         413 my ($why, $long, $data) = $self->_why_invalid( $value );
20 130 100       457 $self->_throw_exception( $why, $long, $data ) if defined $why;
21 49         177 ${$self} = $self->_untaint( $self->value );
  49         101  
22              
23 49         185 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       13 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   2 my ($self, $value) = @_;
39 1         7 return;
40             }
41              
42             # Default exception support just uses die to throw an exception.
43             sub _throw_exception
44             {
45 81     81   133 my ($self, $why, $longmsg, $data) = @_;
46 81         786 die $why;
47             }
48              
49             # Brute force untaint.
50             sub _untaint
51             {
52 49     49   89 my ($self, $value) = @_;
53              
54             # Can only untaint scalars
55 49 50       150 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         151 $value =~ m/\A(.*)\z/sm;
61 49         146 return $1;
62             }
63              
64             1;
65             __END__