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 13     13   71828 use warnings;
  13         27  
  13         430  
4 13     13   64 use strict;
  13         21  
  13         4107  
5              
6             our $VERSION = '0.13';
7              
8             sub value
9             {
10 236     236 1 10253 my ($self) = @_;
11 236         237 return ${$self};
  236         988  
12             }
13              
14             sub new
15             {
16 130     130 1 52728 my ($class, $value) = @_;
17 130         263 my $self = bless \$value, $class;
18              
19 130         327 my ($why, $long, $data) = $self->_why_invalid( $self->value );
20 130 100       432 $self->_throw_exception( $why, $long, $data ) if defined $why;
21 49         131 ${$self} = $self->_untaint( $self->value );
  49         79  
22              
23 49         173 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   42 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         18 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         8 return;
40             }
41              
42             # Default exception support just uses die to throw an exception.
43             sub _throw_exception
44             {
45 81     81   121 my ($self, $why, $longmsg, $data) = @_;
46 81         757 die $why;
47             }
48              
49             # Brute force untaint.
50             sub _untaint
51             {
52 49     49   76 my ($self, $value) = @_;
53              
54             # Can only untaint scalars
55 49 50       136 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         128 $value =~ m/\A(.*)\z/sm;
61 49         127 return $1;
62             }
63              
64             1;
65             __END__