File Coverage

blib/lib/Data/DefGen.pm
Criterion Covered Total %
statement 42 49 85.7
branch 10 18 55.5
condition n/a
subroutine 10 11 90.9
pod 0 3 0.0
total 62 81 76.5


line stmt bran cond sub pod time code
1             package Data::DefGen;
2              
3 1     1   13096 use warnings;
  1         1  
  1         23  
4 1     1   3 use strict;
  1         1  
  1         20  
5 1     1   353 use mro qw(c3);
  1         389  
  1         4  
6              
7             BEGIN {
8 1     1   50 require Exporter;
9 1         3 *import = \&Exporter::import;
10              
11 1         1 our $VERSION = "1.001001";
12 1         21 our @EXPORT = qw(def);
13             }
14              
15 1     1   3 use Scalar::Util qw(reftype blessed);
  1         1  
  1         372  
16              
17             # to subclass, copy and EXPORT this function
18 19     19 0 3286 sub def (&@) { __PACKAGE__->new(data => shift, @_) }
19              
20             sub new {
21 19     19 0 18 my $class = shift;
22 19         26 my $self = bless { }, $class;
23 19         27 $self->_init(@_);
24 19         68 return $self;
25             }
26              
27             sub _init {
28 19     19   12 my $self = shift;
29 19         14 %{ $self } = (
  19         43  
30             data => undef,
31             @_,
32             );
33              
34 0     0   0 $self->{obj_cloner} = sub { $_[0] }
35 19 50       82 unless UNIVERSAL::isa($self->{obj_cloner}, "CODE");
36             }
37              
38             sub gen {
39 7     7 0 6 my $self = shift;
40 7         10 local $self->{gen_p} = \@_;
41 7         8 return $self->_gen($self);
42             }
43              
44             sub _gen {
45 95     95   98 my $self = shift;
46              
47 95 100       160 if (defined blessed($_[0]))
48             {
49 21 50       52 if ($_[0]->isa(ref $self))
50             {
51 21 50       39 return $self->_gen($_[0]->{data}) if ref($_[0]->{data}) ne "CODE";
52              
53 21         13 my @data = @{ $self->_gen([ $_[0]->{data}->(@{ $self->{gen_p} }) ]) };
  21         15  
  21         39  
54 21         94 return @data[0 .. $#data];
55             }
56             else
57             {
58 0         0 return $self->{obj_cloner}->($_[0]);
59             }
60             }
61              
62 74         87 my $type = reftype($_[0]);
63 74 100       150 $type or return $_[0];
64 39 100       50 $type eq "HASH" and return { map +($_ => $self->_gen($_[0]->{$_})), keys %{ $_[0] } };
  7         20  
65 32 50       41 $type eq "ARRAY" and return [ map $self->_gen($_), @{ $_[0] } ];
  32         60  
66 0 0         $type eq "SCALAR" and return \${ $_[0] };
  0            
67 0 0         $type eq "REF" and return \$self->_gen(${ $_[0] });
  0            
68 0           return $_[0];
69             }
70              
71             1;
72              
73              
74             __END__