Diagram.pm

filter: Statementstmt 6.3% 11 / 174 Branchbran 0.0% 0 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 15.7% 3 / 19 Podpod 20.0% 2 / 10 totaltotal 6.2% 16 / 255 CC 40 SCAR 39.1
CC
40
Cov
5%
new
132.0
Render
90.0
_discoverClasses
30.0
CRAP 50.1
↑ Summary Diagram/Render/UXF20.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2
3 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.0 $ =~ /(\d+)\.(\d+)/);
5
6 ### ######################################################################
7 ### ######################################################################
8 #
9 # see? http://www.yy.ics.keio.ac.jp/~suzuki/project/uxf/uxf.html
10 #
11 # see Philip Crow > UML-Sequence-0.04 > UML::Sequence
12 #
13 ### ######################################################################
14 ### ######################################################################
15
16
17 ### ######################################################################
18 ### ######################################################################
19 package Devel::Package; # A UML "package", which is (several) Perl modules
20 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
21 0 my $self = bless { 'Name' => $_[1], 'Classes' => {}, '_filename' => $_[2] };
22 0 return $self;
23 }
24
25
26 ### ######################################################################
27 ### ######################################################################
28 package Devel::Class; # A UML "class" is a Perl "package"
29 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
30 0 my $self = bless { 'Name' => $_[1], 'Attributes' => {}, 'Operations' => {} };
31 0 return $self;
32 }
33
34
35 ### ######################################################################
36 ### ######################################################################
37 package Devel::Attribute; # Discovered by pattern matching
38 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
39 0 my $self = bless { 'Name' => $_[1], 'Type' => $_[2], 'Visibility' => $_[3] };
40 0 return $self;
41 }
42
43
44 ### ######################################################################
45 ### ######################################################################
46 package Devel::Operation; # Discovered as "sub something {"
47 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
48 0 my $self = bless { 'Name' => $_[1], 'Type' => $_[2], 'Visibility' => $_[3] };
49 0 return $self;
50 }
51
52
53 ### ######################################################################
54 ### ######################################################################
55
56
57 ### ######################################################################
58 ### ######################################################################
59 ### ######################################################################
60 ### ######################################################################
61 ### ######################################################################
62 package Devel::Diagram; # A container for all the stuff we'll discover here.
63 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
64
65 ### ######################################################################
66 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
67 0 my $self = bless { 'Name' => $_[1], 'Packages' => {}, '_isDiscovered' => 0 }, shift;
68
69 0 for ( @_ ) {
70 0 my $filnam = $_;
71 0 $filnam =~ s{::}{/}g;
72 0 my $moduleName = $filnam;
73 0 $moduleName =~ s{/$}{}; $moduleName =~ s{/}{::}g;
74
75 0 my $foundIt = 0;
76 0 for my $lib (@INC) {
77 0 ▶ if ( -f "$lib/$filnam.pm" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$lib/$filnam.pm") 0 0 0
78 0 $moduleName = "$filnam"; $moduleName =~ s{/$}{}; $moduleName =~ s{/}{::}g;
79 0 $self->{'Packages'}->{$moduleName} = new Devel::Package($moduleName, "$lib/$filnam.pm");
80 0 $self->{'Packages'}->{$moduleName}->{'_filename'} = "$lib/$filnam.pm";
81 0 $foundIt = 1;
82 }
83
84 0 ▶ if ( -d "$lib/$filnam" ) { # e.g. HTML/ - HTML has no HTML.pm file.
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$lib/$filnam") 0 0 0
85 0 ▶ $filnam .= '/' unless $filnam =~ m{/$}; # include the module's folder.
Branch: 1 branch
BranchTrueFalseTotal
unless $filnam =~ m[/$] 0 0 0
86 0 my $fh = new FileHandle;
87 0 opendir $fh, "$lib/$filnam";
88 0 ▶ while ( my $fil = readdir $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fil = readdir $fh)) 0 0 0
89 0 ▶ if ( $fil =~ s{\.pm$}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fil =~ s/\.pm$//) 0 0 0
90 0 $moduleName = "$filnam$fil"; $moduleName =~ s{/$}{}; $moduleName =~ s{/}{::}g;
91 0 my $subModule = new Devel::Diagram($moduleName);
92 # Merge the "packages" of the sub-module into ours.
93 0 for ( keys %{$subModule->{'Packages'}} ) {
94 0 ▶ if ( $self->{'Packages'}->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Packages'}{$_}) { } 0 0 0
95 0 warn <<EOT;
96 $moduleName contains new or redefined operations/attributes of $_.
97 Devel::Diagram is not yet robust enough to merge these two definitions, so
98 operations/attributes of $_ that are defined in $moduleName will be lost.
99 EOT
100 } else {
101 0 $self->{'Packages'}->{$_} = $subModule->{'Packages'}->{$_};
102 }
103 }
104 }
105 }
106 0 closedir $fh;
107 0 $foundIt = 1;
108 }
109 0 ▶ last if $foundIt;
Branch: 1 branch
BranchTrueFalseTotal
if $foundIt 0 0 0
110 }
111 }
112 0 return $self;
113 }
114
115
116 ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
117 0 ▶sub _discoverClasses {
Subroutine _discoverClasses: not called CC=5 SCAR=34.0
118 0 my $self = shift;
119
120 0 for ( keys %{$self->{'Packages'}} ) {
121 0 my $moduleName = $_; # '$_' is read-only.
122 0 my $module = $self->{'Packages'}->{$_};
123
124 0 my $filnam = $module->{'Name'};
125 0 $filnam =~ s{::}{/}g;
126 0 $filnam =~ s{'}{/}g;
127 0 $self->_discoverClass($module);
128
129 # Now recurse into any module that this one ISA.
130 0 for my $uses ( sort keys %{$self->{'Packages'}->{$moduleName}->{'_uses'}} ) {
131 # Only if the named package is based on one we've done before, then recurse into it.
132 0 for ( keys %{$self->{'Packages'}} ) {
133 0 ▶ if ( $uses =~ m{^$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uses =~ /^$_/) 0 0 0
134 0 my $fil = $_;
135 0 $fil =~ s{::}{/}g;
136 0 $fil =~ s{'}{/}g;
137 0 $self->_discoverClass($fil);
138 }
139 }
140 }
141 }
142 }
143
144
145 ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
146 0 ▶sub _discoverClass {
Subroutine _discoverClass: not called CC=5 SCAR=34.0
147 0 my ($self, $module) = @_;
148 0 my $filnam = $module->{'_filename'};
149 0 my $moduleName = $module->{'Name'};
150
151 0 my $packages = $self->{'Packages'};
152
153 # for my $pkgName ( keys %$packages ) {
154 # Slurp the whole file.
155 # next unless $filnam; # Where do these blank filenames come from?
156 0 ▶ open MOD, "<$filnam" or do { warn "Can't read '$filnam': $!"; return; };
Branch: 1 branch
BranchTrueFalseTotal
open MOD, "<$filnam" or do { warn "Can't read '${filnam}': $!"; return } 0 0 0
157 0 my $mod = join '', <MOD>; close MOD;
158
159 0 $self->FindAnnotations(\$mod); # Find any annotations we can discover.
160 0 $self->CleanUpCode(\$mod); # Clean up code, e.g., remove comments.
161
162 #while ( $mod =~ m{\n\s*package\s+(.*?);(.*?)(\n\s*package|$)}gs ) {
163 0 for my $pckg ( $self->FindPackages(\$mod) ) {
164 0 my ($className, $cod) = @$pckg;
165
166 0 ▶ next if $className eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $className eq "main" 0 0 0
167
168 0 my $thisModule = $packages->{$moduleName};
169 $thisModule->{'Classes'}->{$className} = new Devel::Class($className)
170 0 ▶ unless defined $thisModule->{'Classes'}->{$className};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thisModule->{"Classes"}{$className} 0 0 0
171 0 my $thisClass = $thisModule->{'Classes'}->{$className};
172 0 $thisClass->{'_filnam'} = $filnam; # The source file of this package.
173
174 # Find other class components by investigating use's, use base's, etc.
175 0 $self->FindOtherComponents($thisClass, \$cod);
176
177 # Find base classes by investigating @ISA's.
178 0 $self->FindBaseClasses($thisClass, \$cod);
179
180 # Find methods by investigating sub's.
181 0 $self->FindMethods($thisClass, \$cod);
182
183 # Find properties by investigating "$self->{}".
184 0 $self->FindPropertys($thisClass, \$cod);
185 }
186 # }
187 }
188
189 #######################################################################################
190 0 ▶sub FindAnnotations {
Subroutine FindAnnotations: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my ($self, $mod) = @_;
192 }
193
194 #######################################################################################
195 0 ▶sub CleanUpCode {
Subroutine CleanUpCode: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my ($self, $mod) = @_;
197
198 # Remove comments.
199 0 $$mod =~ s{\#.*?\n}{\n}gs;
200 0 $$mod =~ s{=(pod|item|head\d).*?=cut}{}gs;
201 0 $$mod =~ s{\_\_END\_\_.*$}{}gs;
202
203 }
204
205 #######################################################################################
206 0 ▶sub FindPackages {
Subroutine FindPackages: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my ($self, $mod) = @_;
208 0 my @mods;
209 0 ▶ while ( $$mod =~ m{(?:^|\n)\s*package\s+(\w[^\s]+?)\s*;(.*?)(?=\n\s*package|$)}gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$mod =~ /(?:^|\n)\s*package\s+(\w[^\s]+?)\s*;(.*?)(?=\n\s*package|$)/gs) 0 0 0
210 0 my ($nam, $cod) = ($1,$2);
211 0 push @mods, [$nam,$cod];
212 }
213 0 return @mods;
214 }
215
216 #######################################################################################
217 0 ▶sub FindOtherComponents {
Subroutine FindOtherComponents: not called CC=3 SCAR=24.8
Pod undocumented
218 0 my ($self, $packag, $cod) = @_;
219
220 # Find other package components by investigating use's.
221 0 ▶ while ( $$cod =~ m{use\s+([^;]+)\s*;}gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cod =~ /use\s+([^;]+)\s*;/gs) 0 0 0
222 0 my $usee = $1;
223 0 ▶ next if $usee =~ m{^(vars|constant)};
Branch: 1 branch
BranchTrueFalseTotal
if $usee =~ /^(vars|constant)/ 0 0 0
224 0 $packag->{'_uses'}->{$usee} = 1;
225 }
226
227 }
228
229 #######################################################################################
230 0 ▶sub FindBaseClasses {
Subroutine FindBaseClasses: not called CC=3 SCAR=24.8
Pod undocumented
231 0 my ($self, $packag, $cod) = @_;
232
233 # Find base classes by investigating @ISA's and use base's, etc.
234 0 ▶ while ( $$cod =~ m{\@ISA\s*=\s*qw\(\s*([^)]+?\s*)\)\s*;}gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cod =~ /\@ISA\s*=\s*qw\(\s*([^)]+?\s*)\)\s*;/gs) 0 0 0
235 0 my $isa = $1;
236 0 for ( split /\s+/,$isa ) {
237 #print "ISA $_\n";
238 0 $packag->{_isa}->{$_} = 1;
239 #$packages->{$packag}->{_uses}->{$_} = 1;
240 }
241 }
242 # TODO: Find base classes by investigating "use base".
243 }
244
245 #######################################################################################
246 0 ▶sub FindMethods {
Subroutine FindMethods: not called CC=2 SCAR=17.9
Pod undocumented
247 0 my ($self, $clas, $cod) = @_;
248
249 # Find methods by investigating sub's.
250 0 my $methods = $clas->{'Operations'};
251 0 ▶ while ( $$cod =~ m{\n\s*sub\s+([^\{ \n]+)\s*\{(.*?)(\n\s*sub|$)}gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cod =~ /\n\s*sub\s+([^\{ \n]+)\s*\{(.*?)(\n\s*sub|$)/gs) 0 0 0
252 0 $methods->{$1} = new Devel::Operation($1);
253 }
254 }
255
256 #######################################################################################
257 0 ▶sub FindPropertys {
Subroutine FindPropertys: not called CC=4 SCAR=30.0
Pod undocumented
258 0 my ($self, $packag, $cod) = @_;
259
260 # Find properties by investigating "$self->{}".
261 0 my $attributes = $packag->{'Attributes'};
262 0 ▶ while ( $$cod =~ m{\$self->\{['"]?([_a-zA-Z0-9\*]+)["']?\}}gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cod =~ /\$self->\{['"]?([_a-zA-Z0-9\*]+)["']?\}/gs) 0 0 0
263 0 my $attr = $1;
264 0 ▶ $attributes->{$attr} = new Devel::Attribute($attr) unless $attributes->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes->{$attr} 0 0 0
265 0 ▶ $attributes->{$attr}->{'Visibility'} = ($attr =~ m{^_})?'private':'public';
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^_/ ? : 0 0 0
266
267 }
268 }
269
270
271
272 #######################################################################################
273 #######################################################################################
274 0 ▶sub Render {
Subroutine Render: not called CC=9 SCAR=45.0
Pod documented
275 0 my ($self, $renderType, $transform) = @_;
276
277 0 ▶ die "Unrecognized rendering type '$renderType'" unless $renderType =~ m{^(UXF20)$};
Branch: 1 branch
BranchTrueFalseTotal
unless $renderType =~ /^(UXF20)$/ 0 0 0
278
279 0 ▶ $self->_discoverClasses() unless $self->{'_isDiscovered'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_isDiscovered"} 0 0 0
280
281 0 my $render;
282 0 eval "require Devel::Diagram::Render::$renderType;
283 \$render = Render Devel::Diagram::Render::$renderType(\$self)";
284 0 ▶ return $render if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
285
286 0 ▶ if ( $transform ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transform) 0 0 0
287 0 ▶ if ( $transform =~ m{^xsl\:(.+)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transform =~ /^xsl\:(.+)$/) 0 0 0
288 0 my $xsl = $1;
289 0 $xsl =~ s{\.xsl$}{}i;
290 0 for my $lib (@INC) {
291 0 ▶ if ( -f "$lib/Devel/Diagram.pm" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$lib/Devel/Diagram.pm") 0 0 0
292 0 ▶ if ( -f "$lib/Devel/Diagram/xsl/$xsl.xsl" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$lib/Devel/Diagram/xsl/$xsl.xsl") { } 0 0 0
293 0 my $tempXml = 'develDiagram.temp.xml';
294 0 open TMP, ">$tempXml";
295 0 print TMP $render;
296 0 close TMP;
297 0 eval " use XML::XSLT::Wrapper;
298 my \$xslt = XML::XSLT::Wrapper->new();
299 \$render = \$xslt->transform(
300 XMLFile => '$tempXml',
301 XSLFile => '$lib/Devel/Diagram/xsl/$xsl.xsl');
302 ";
303 0 unlink $tempXml;
304 0 $render =~ s{^.*?<\?xml version="1.0" encoding="UTF-8"\?>\s*}{}si;
305 0 return $render;
306 } else {
307 0 eval "die 'Can not find transform file $xsl.xsl\nThis needs to be in $lib/Devel/Diagram/xsl\n'";
308 0 return $render;
309 }
310 }
311 0 eval "die 'Can not find root of Devel::Diagram\nYou did something with \"use lib\" or \"\@INC\"?\n'";
312 }
313 }
314 }
315
316 0 return $render;
317 }
318
319
320 #######################################################################################
321 #######################################################################################
322 0 ▶sub PrintAsHtml {
Subroutine PrintAsHtml: not called CC=5 SCAR=34.0
Pod undocumented
323 0 my $self = shift;
324 0 my $packages = $self->{packages};
325
326 0 open XML, ">Diagram.html";
327 0 print XML <<EOT;
328 <html><head>
329 <style>
330 .tr { valign:top; }
331 .td { valign:top; }
332 </style>
333 </head><body>
334 EOT
335 0 print XML "<table border='1'>\n";
336
337 0 for my $packnam ( sort keys %$packages ) {
338 0 print XML "<tr class='tr'><td class='td' valign='top'>$packnam</td>\n";
339 0 print XML "<td class='td' valign='top'><table>\n";
340 0 for my $baseclass ( sort keys %{$packages->{$packnam}->{_isa}} ) {
341 0 print XML "<tr class='tr'><td class='td' valign='top'>$baseclass</td></tr>\n";
342 }
343 0 print XML "</table></td>\n";
344 0 print XML "<td class='td' valign='top'><table>\n";
345 0 for my $method ( sort keys %{$packages->{$packnam}->{_methods}} ) {
346 0 print XML "<tr class='tr'><td class='td' valign='top'>$method</td></tr>\n";
347 }
348 0 print XML "</table></td>\n";
349 0 print XML "<td class='td' valign='top'><table>\n";
350 0 for my $member ( sort keys %{$packages->{$packnam}->{_members}} ) {
351 0 print XML "<tr class='tr'><td class='td' valign='top'>$member</td></tr>\n";
352 }
353 0 print XML "</table></td></tr>\n";
354 }
355 0 print XML "</table></body></html>\n";
356 0 close XML;
357 }
358
359 1 1;
360
361
362
363 =pod
364
365 =head1 NAME
366
367 Devel::Diagram - Discover the classes of an arbitrary suite of Perl modules
368
369 =head1 SYNOPSIS
370
371 use Devel::Diagram;
372
373 # Discover classes of a package anchored by a single Perl module.
374 #
375 $diagram = new Devel::Diagram('CGI');
376
377 # Discover classes of a package anchored by a collection of modules in a folder.
378 #
379 use Devel::Diagram;
380 $diagram = new Devel::Diagram('HTML/');
381
382 # Render the result in your desired format.
383 #
384 print $diagram->Render('UXF20');
385
386 # Render the result, then transform it via XSL.
387 #
388 print $diagram->Render('UXF20', 'xsl:uxf20toHtml');
389
390 =head1 DESCRIPTION
391
392 Devel::Diagram scans the given Perl modules attempting to discover the class structure.
393 It produces a hash table that can be converted to XML (or other formats) via Render().
394
395 An XSL stylesheet is included that converts the XML class diagram into HTML.
396
397 See C<eg/Diagram.pl> for a full example of use.
398
399 =head1 METHODS
400
401 The few methods you need to activate Devel::Diagram.
402
403 =head3 new( $moduleSpecifications )
404
405 Here you name the Perl module (or suite) you want to process.
406 Enter the string you would specify in a 'use' or 'require' statement for this module.
407
408 You may enter as many module specifications as you like, separated by commas.
409
410 =head3 Render( $renderType [, $transformType] )
411
412 Renders the class diagram in the given format.
413 Currently the only format that is recognized is 'UXF20'.
414 These can be extended easily by creating a new C<Devel::Diagram::Render::<yourName>> module.
415
416 Render() optionally takes a second parameter specifying a transformation on the rendered
417 format, presumably resulting in a new format. For instance,
418
419 Render('UXF20', 'xsl:uxf20toHtml')
420
421 renders the class diagram as UXF20, then runs it through the XSL transform named C<uxf20toHtml.xsl>.
422
423 C<Render()> expects to find the XSL stylesheet in the C<xsl> folder of C<Devel::Diagram>.
424 You need C<XML::XSLT::Wrapper> and an appropriate XSL transform engine to make this work.
425
426 Any warnings or errors in the rendering process can be found by investigating C<$@> on return.
427
428 =head1 TODO
429
430 These are some of the things I think can be done to extend Devel::Diagram.
431
432 =over 4
433
434 =item XMI format
435
436 Currently C<UXF> is the only XML format supported. C<XMI> is another commonly used format (but more complex).
437
438 =item Fancy HTML rendering
439
440 Perhaps with Javascript and/or server side to assist in browsing the codebase.
441
442 =item Class::Struct parsing
443
444 Class::Struct is also used to code OO Perl. Need to recognize this structure in the codebase.
445 There are also several other modules for class creation.
446
447 =item Parameters
448
449 What are the parameters of the operations?
450
451 =item Other parsing
452
453 The is more than one way to do it. OO Perl can be implemented in many ways;
454 Devel::Diagram recognizes a few of them.
455 CPAN is big, really big, so there are OO Perl techniques that Devel::Diagram will not recognize, yet.
456
457 =item Other UML diagrams
458
459 Collaboration, sequence, etc. (see C<UML::Sequence>).
460
461 =item Devel::Diagram all modules of CPAN
462
463 Anybody?
464
465 =back
466
467 =head1 AUTHOR
468
469 C<Devel::Diagram> is written and maintained
470 by Glenn Wood, http://search.cpan.org/search?mode=author&query=GLENNWOOD.
471
472 =head1 COPYRIGHT
473
474 Copyright (c) 2003 Glenn Wood
475 All rights reserved.
476
477 This program is free software; you can redistribute it and/or
478 modify it under the same terms as Perl itself.
479
480
481 =cut
482
483
↑ Summary Diagram/Render/UXF20.pm »