File Coverage

blib/lib/Waft/StashAccessor.pm
Criterion Covered Total %
statement 48 48 100.0
branch 9 10 90.0
condition n/a
subroutine 12 12 100.0
pod 0 2 0.0
total 69 72 95.8


line stmt bran cond sub pod time code
1             package Waft::StashAccessor;
2              
3 2     2   110274 use 5.005;
  2         8  
  2         79  
4 2     2   12 use base 'Exporter';
  2         4  
  2         153  
5 2     2   11 use strict;
  2         7  
  2         58  
6 2     2   9 use vars qw( $VERSION @EXPORT );
  2         4  
  2         175  
7 2 50   2   4 BEGIN { eval { require warnings } ? 'warnings'->import : ( $^W = 1 ) }
  2         57  
8              
9 2     2   11 use Carp;
  2         2  
  2         128  
10 2     2   11 use Waft ();
  2         3  
  2         535  
11              
12             $VERSION = '0.01';
13             $VERSION = eval $VERSION;
14              
15             @EXPORT = qw( make_stash_accessor );
16              
17             sub make_stash_accessor {
18 2     2 0 28 my (@keys) = @_;
19 2 100       10 my $option = ref $keys[-1] eq 'HASH' ? pop @keys
20             : {};
21              
22 2         14 my $caller = caller;
23              
24 2         6 my $minlen = $option->{minlen};
25              
26 2         8 for my $key ( @keys ) {
27             my $setter = sub {
28 6     6   260 my ($self, $value) = @_;
29              
30 6         13 validate($value, $minlen);
31              
32 5         22 $self->stash($caller)->{$key} = $value;
33              
34 5         60 return;
35 3         16 };
36              
37             my $getter = sub {
38 8     8   519 my ($self) = @_;
39              
40 8         20 my $value = $self->stash($caller)->{$key};
41              
42 8         54 validate($value, $minlen);
43              
44 6         19 return $value;
45 3         16 };
46              
47 2     2   12 no strict 'refs';
  2         4  
  2         305  
48 3         7 *{ "${caller}::set_$key" } = $setter;
  3         20  
49 3         5 *{ "${caller}::get_$key" } = $getter;
  3         18  
50             }
51              
52 2         7 return;
53             }
54              
55             sub validate {
56 14     14 0 19 my ($value, $minlen) = @_;
57              
58 14 100       30 if ( $minlen ) {
59 10 100       124 croak 'Use of uninitialized value' if not defined $value;
60 9 100       324 croak 'Less than minimum length' if length $value < $minlen;
61             }
62              
63 11         18 return;
64             }
65              
66             1;
67             __END__