lib/GraphViz/ISA/Multi.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1  
2 package GraphViz::ISA::Multi;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use GraphViz;
Subroutine BEGIN: called CC=1 SCAR=2.6
5  
6 use Carp;
7  
8 BEGIN {
9 use Exporter ();
10 use vars qw ($VERSION @ISA);
11 $VERSION = 0.02;
12 }
13  
14 sub new
15 {
16 my ($class, %parameters) = @_;
17  
18 my $self = bless ({}, ref ($class) || $class);
19  
20 $self->{data} = {};
21 $self->{ignore} = $parameters{ignore};
22 $self->{changed} = 0;
23 return ($self);
24 }
25  
26  
27 sub graph
28 {
29 my $self = shift;
30 return $self->{g} unless $self->{changed};
31  
32 $self->{g} = GraphViz->new();
33  
34 ### draw all nodes:
35 foreach my $module (sort keys %{$self->{data}}) {
36 $self->{g}->add_node($module);
37 foreach my $nod (@{$self->{data}->{$module}}) {
38 $self->{g}->add_node($nod) ;
39 }
40 }
41  
42 ### draw the edges
43 foreach my $module (sort keys %{$self->{data}}) {
44 $self->{g}->add_edge($_, $module) for
45 @{$self->{data}->{$module}};
46 }
47 $self->{changed} = 0;
48  
49 return $self->{g};
50 }
51  
52  
53 sub add
54 {
55 my $self = shift;
56 my @to_add = @_;
57  
58 foreach my $module (@to_add) {
59 next if grep /$module/i, @{$self->{ignore}};
60 next if $self->{data}->{$module};
61 my $filename = $module;
62 $filename =~ s!::!/!g;
63 $filename .= ".pm";
64  
65 my @pkg = ($module);
66 eval {
67 require $filename;
68 # if we got more packages in a file, find them
69 open my $fg, "<$INC{$filename}" or croak "$!\n";
70 foreach my $line (<$fg>) {
71 if ($line =~ /package (.+);/) {
72 push @pkg, $1 unless $1 eq $module;
73 }
74 }
75 close $fg;
76 };
77 if ($@) {
78 return undef;
79 }
80  
81 no strict 'refs';
82 foreach my $mod (@pkg) {
83 if (@{$mod . "::ISA"} > 0) {
84 $self->{data}->{$mod} = \@{$mod."::ISA"};
85 foreach my $ign (@{$self->{ignore}}) {
86 @{$self->{data}->{$mod}} =
87 grep $_ !~ /$ign/, @{$self->{data}->{$mod}};
88 }
89 $self->add($_) foreach @{$mod."::ISA"};
90 }
91 }
92 }
93 $self->{changed} = 1;
94 return $self->{data};
95 }
96  
97 sub AUTOLOAD
98 {
99 no strict 'vars';
100 my $self = shift;
101 my $n = $AUTOLOAD;
102 $n =~ s/.*:://g;
103  
104 return if $n =~ /DESTROY/;
105  
106 if ($n =~ /as_/) {
107 $self->graph();
108
109 }
110 ### give it down to GraphViz:
111 $self->{g}->$n(@_);
112 }
113  
114  
115 1; #this line is important and will help the module return a true value
116 __END__
↑ Summary