blib/lib/Class/Serializer.pm

filter: Statementstmt 60.4% 81 / 134 Branchbran 36.1% 13 / 36 Conditioncond 25.0% 6 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 82.3% 14 / 17 Podpod 100% 2 / 2 totaltotal 51.5% 118 / 229 CC 35 SCAR 30.1
CC
35
Cov
52%
__ANON__
29.1
as_string
29.1
__ANON__
23.3
CRAP 20.3
↑ Summary
1 package Class::Serializer;
2
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5
6 # no imports, thanks
7 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 2 ▶use Data::Dump::Streamer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9
10 =head1 NAME
11
12 Class::Serializer - Serializes the in-memory state of a class into code
13
14 =head1 VERSION
15
16 Version 0.04
17
18 =cut
19
20 3 our $VERSION = '0.04';
21
22 =head1 SYNOPSIS
23
24 This module does its best efforts to serialize the in-memory state of a class
25 into runable code. For this to actually happen successfully it relies heavily
26 on L<Data::Dump::Streamer> which, in turn, relies on L<B::Deparse> for
27 CODEREF deparsing and other similar tasks.
28
29 B<Please, keep in mind that this module is experimental.
30 There are no guarantees whatsoever about the generated code.>
31
32 With that said, it should work just fine.
33
34 Here's a little a code snippet:
35
36 use Class::Serializer;
37
38 # Class::Serializer is Class::Serializer safe
39 my $class_code = Class::Serializer->as_string('Class::Serializer');
40
41 # writes directly to ClassSerializer.pm
42 Class::Serializer->as_file(Class::Serializer => 'ClassSerializer.pm');
43
44 =head1 CLASS METHODS
45
46 =cut
47
48 =head2 as_string($target_class)
49
50 Serializes C<$target_class> in-memory state (actually, symbol table entries)
51 into perl code and returns it as a string. It will also try to detect possible
52 dependencies and try to honor them through C<require> statements in the
53 generated code.
54
55 =cut
56 0 ▶sub as_string {
Subroutine as_string: not called CC=12 SCAR=33.7
Subroutine __ANON__: called CC=12 SCAR=33.7
Pod documented
57 0 my $class = shift;
58 0 my ($target) = @_;
59
60 0 my %seen;
61 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
62
63 # loads the relevant data structures
64 0 ▶ while (my ($entry, $contents) = each %{"${target}::"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{"${target}::";}) 0 0 0
if (() = each %{"${target}::";}) 22 2 24
65 0 for my $type (qw|SCALAR ARRAY HASH CODE|) {
66 0 ▶ if (*{$contents}{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (*{$contents;}{$type}) 0 0 0
if (*{$contents;}{$type}) 30 58 88
67 0 ▶ next if ($type eq 'SCALAR' && !defined ${*{$contents}{$type}});
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "SCALAR" and not defined ${*{$contents;}{$type};} 0 0 0
if $type eq "SCALAR" and not defined ${*{$contents;}{$type};} 12 18 30
Condition50%3/6
$type eq "SCALAR" and not defined ${*{$contents;}{$type};}
!ll&&!rl&&r
000
$type eq "SCALAR" and not defined ${*{$contents;}{$type};}
!ll&&!rl&&r
81012
MC/DC0%0/2
$type eq "SCALAR" and not defined ${*{$contents;}{$type};}
$type eq "SCALAR" not defined ${*{$contents;}{$type};}
MC/DC100%2/2
$type eq "SCALAR" and not defined ${*{$contents;}{$type};}
$type eq "SCALAR" not defined ${*{$contents;}{$type};}
Truth table0%0/3
A and not(B)
A= $type eq "SCALAR"B= not defined ${*{$contents;}{$type};}
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= $type eq "SCALAR"B= not defined ${*{$contents;}{$type};}
ABresult
0X0
100
111
68 0 push(@{$seen{$type}}, ["$entry", *{$contents}{$type}]);
69 }
70 }
71 };
72
73 2 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
74
75 # builds up something suitable to be spoon fed to the dumper
76 0 my (@dump, @names);
77 0 for my $type (qw|ARRAY HASH CODE|) {
78 0 for my $entry (@{$seen{$type}}) {
79 0 push(@dump, $entry->[1]);
80 0 push(@names, '*'.$entry->[0]);
81 }
82 }
83
84 # Data::Dumper messes everything up with scalars
85 0 for my $entry (@{$seen{SCALAR}}) {
86 0 push(@dump, ${$entry->[1]});
87 0 push(@names, '$'.$entry->[0]);
88 }
89
90 my $dump =
91 "package $target;\n" .
92 0 Data::Dump::Streamer::DumpVars( map { $names[$_], $dump[$_] } 0..$#dump )->Declare(1)->Out() .
93 ';1;';
94
95 0 my %required = ($target => 1);
96 # tries to detect dependencies and loads them through eval 'require Pkg'
97 # (eval is used so that errors are not fatal)
98 0 my $require = '';
99 0 ▶ while ($dump =~ /(?:package ([\w\:]+);|'([\w\:]+)'\->)/g) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dump =~ /(?:package ([\w\:]+);|'([\w\:]+)'\->)/g) 0 0 0
if ($dump =~ /(?:package ([\w\:]+);|'([\w\:]+)'\->)/g) 8 2 10
100 0 ▶ my $pkg = $1 || $2;
Condition16%1/6
$1 || $2
l!l&&r!l&&!r
000
$1 || $2
l!l&&r!l&&!r
800
MC/DC0%0/2
$1 || $2
$1 $2
MC/DC0%0/2
$1 || $2
$1 $2
Truth table0%0/3
A || B
A= $1B= $2
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
101 0 ▶ unless ($required{$pkg}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($required{$pkg}) 0 0 0
unless ($required{$pkg}) 0 8 8
102 0 $require .= "eval 'require $pkg';\n";
103 0 $required{$pkg} = 1;
104 }
105 }
106
107 0 $require . $dump;
108
109 }
110
111 =head2 as_file($target_class, [$file_name, [$overwrite]])
112
113 Serializes C<$target_class> in-memory state into perl code and saves it into
114 C<$file_name>, overwriting the file if C<$overwrite> is set to a true value.
115
116 If C<$file_name> is not defined, it will be constructed based on the target
117 class name, relative do the current path. So C<Class::Serializer> would be saved
118 in Class/Serializer.pm.
119
120 If C<$file_name> exists and C<$overwrite> is not set, an exception is thrown.
121 An exception is also thrown if the file is not writable.
122
123 =cut
124
125 0 ▶sub as_file {
Subroutine as_file: not called CC=7 SCAR=31.5
Subroutine __ANON__: called CC=7 SCAR=31.5
Pod documented
126 0 my $class = shift;
127 0 my ($target, $file_name, $overwrite) = @_;
128
129 # constructs the file name, if it's either undef or empty
130 0 ▶ unless (defined $file_name && length($file_name)) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $file_name and length $file_name) 0 0 0
unless (defined $file_name and length $file_name) 0 1 1
Condition16%1/6
defined $file_name and length $file_name
!ll&&!rl&&r
000
defined $file_name and length $file_name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file_name and length $file_name
defined $file_name length $file_name
MC/DC0%0/2
defined $file_name and length $file_name
defined $file_name length $file_name
Truth table0%0/3
A and B
A= defined $file_nameB= length $file_name
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $file_nameB= length $file_name
ABresult
0X0
100
111
131 0 ($file_name = $target) =~ s|::|/|;
132 0 $file_name .= '.pm';
133 }
134
135 # creates directories if they don't exist
136 0 ▶ if ((my $path = $file_name) =~ s|([\\/])[^\\/]+$|$1|) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $path = $file_name) =~ s[([\\/])[^\\/]+$][$1]) 0 0 0
if ((my $path = $file_name) =~ s[([\\/])[^\\/]+$][$1]) 1 0 1
137 0 File::Path::mkpath($path);
138 }
139
140 0 ▶ _croak("'$file_name' already exists")
Branch: 2 branches
BranchTrueFalseTotal
if -e $file_name and not $overwrite 0 0 0
if -e $file_name and not $overwrite 0 1 1
Condition16%1/6
-e $file_name and not $overwrite
!ll&&!rl&&r
000
-e $file_name and not $overwrite
!ll&&!rl&&r
100
MC/DC0%0/2
-e $file_name and not $overwrite
-e $file_name not $overwrite
MC/DC0%0/2
-e $file_name and not $overwrite
-e $file_name not $overwrite
Truth table0%0/3
A and not(B)
A= -e $file_nameB= not $overwrite
ABresult
0X0
100
111
Truth table33%1/3
A and not(B)
A= -e $file_nameB= not $overwrite
ABresult
0X0
100
111
141 if (-e $file_name && !$overwrite);
142
143 # writes
144 0 ▶ open(my $fh, '>', $file_name)
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, ">", $file_name or _croak("couldn't write to '${file_name}': $!") 0 0 0
open my $fh, ">", $file_name or _croak("couldn't write to '${file_name}': $!") 1 0 1
145 or _croak("couldn't write to '$file_name': $!");
146
147 0 print $fh $class->as_string($target);
148
149 0 close $fh;
150
151 0 $file_name;
152 }
153
154 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
155 0 require Carp;
156 0 Carp::croak(@_);
157 }
158
159 =head1 CAVEATS
160
161 The dependency detecting code is pretty simple and may be not very reliable.
162
163 Closures should work just fine as of version 0.04. This feature wasn't tested
164 extensively, though (it just relies on L<Data::Dump::Streamer> for that).
165
166 =head1 AUTHOR
167
168 Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
169
170 =head1 BUGS
171
172 Please report any bugs or feature requests directly to the author.
173 If you ask nicely it will probably get fixed or implemented.
174
175 =head1 SUPPORT
176
177 You can also look for information at:
178
179 =over 4
180
181 =item * AnnoCPAN: Annotated CPAN documentation
182
183 L<http://annocpan.org/dist/Class-Serializer>
184
185 =item * CPAN Ratings
186
187 L<http://cpanratings.perl.org/d/Class-Serializer>
188
189 =item * Search CPAN
190
191 L<http://search.cpan.org/dist/Class-Serializer>
192
193 =back
194
195 =head1 SEE ALSO
196
197 L<Data::Dump::Streamer>, L<B::Deparse>
198
199 =head1 COPYRIGHT & LICENSE
200
201 Copyright 2008 Nilson Santos Figueiredo Junior, all rights reserved.
202
203 This program is free software; you can redistribute it and/or modify it
204 under the same terms as Perl itself.
205
206 =cut
207
208 3 1; # End of Class::Serializer
↑ Summary