| 1 | package Devel::OOCallSeq; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | OOCallSeq - produces call sequence outlines (like dprofpp -T) | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | perl -d:OOCallSeq script | ||||||||||||||||||||||||||||
| 10 | cat tmon.out | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | This profiler is designed to aid in the production of UML sequence diagrams. | ||||||||||||||||||||||||||||
| 15 | It is used by UML::Sequence through genericseq.pl and UML::Sequence::PerlOOSeq. | ||||||||||||||||||||||||||||
| 16 | (See UML::Sequence::PerlOOSeq for how to use it in practice.) | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | The key differences between the output of this program and dprofpp -T are | ||||||||||||||||||||||||||||
| 19 | these: | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | subroutine names show up correctly (DiePair::new not DiePair::BEGIN) | ||||||||||||||||||||||||||||
| 22 | subroutine names are preceded by instance names (die1:Die::roll) | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Output goes to tmon.out and looks like this: | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | main | ||||||||||||||||||||||||||||
| 27 | diePair1:DiePair::new | ||||||||||||||||||||||||||||
| 28 | die1:Die::new | ||||||||||||||||||||||||||||
| 29 | die2:Die::new | ||||||||||||||||||||||||||||
| 30 | diePair1:DiePair::roll | ||||||||||||||||||||||||||||
| 31 | die1:Die::roll | ||||||||||||||||||||||||||||
| 32 | die2:Die::roll | ||||||||||||||||||||||||||||
| 33 | diePair1:DiePair::total | ||||||||||||||||||||||||||||
| 34 | diePair1:DiePair::doubles | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | package DB; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # %next_object_number is keyed by package name storing number of objects seen | ||||||||||||||||||||||||||||
| 41 | # in it so far (to use it preincrement it) | ||||||||||||||||||||||||||||
| 42 | our %next_object_number; | ||||||||||||||||||||||||||||
| 43 | # %objects is keyed by object reference (like HASH(0x1a6f26c)) storing the | ||||||||||||||||||||||||||||
| 44 | # number of the object in its class | ||||||||||||||||||||||||||||
| 45 | our %objects; | ||||||||||||||||||||||||||||
| 46 | our $stack_depth = 0; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | our $previous_frame; | ||||||||||||||||||||||||||||
| 49 | our $depth; | ||||||||||||||||||||||||||||
| 50 | our @output; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | BEGIN { | ||||||||||||||||||||||||||||
| 53 | $single = 0; | ||||||||||||||||||||||||||||
| 54 | open TMON, ">tmon.out" or die "Couldn't open tmon.out\n"; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | END { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | local $" = ""; | ||||||||||||||||||||||||||
|
Subroutine
END: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | print TMON "@output"; | |||||||||||||||||||||||||||
| 59 | 0 | close TMON; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub sub { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | local $stack_depth = $stack_depth + 1; | ||||||||||||||||||||||||||
|
Subroutine
sub: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (wantarray) { # list context | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 0 | @ret = &$DB::sub; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | elsif (defined wantarray) { # scalar context | ||||||||||||||||||||||||||||
| 69 | 0 | $ret = &$DB::sub; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (defined $ret and "$ret" =~ /([^=]*)=(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $ret and "$ret" =~ /([^=]*)=(.*)/
MC/DC0%0/2
defined $ret and "$ret" =~ /([^=]*)=(.*)/
defined $ret
"$ret" =~ /([^=]*)=(.*)/
Truth table0%0/3
A and B
A= defined $retB= "$ret" =~ /([^=]*)=(.*)/
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($type, $key) = ($1, $2); | |||||||||||||||||||||||||||
| 72 | # put it in the hash | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless (defined $objects{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $objects{$key} = ++$next_object_number{$type}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | # to find the constructor, walk back through the output until we | ||||||||||||||||||||||||||||
| 77 | # see the most recent call at the current stack level | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | for (my $i = scalar @output - 1; $i >= 0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $output[$i] =~ /(\s+)(.*)/; | |||||||||||||||||||||||||||
| 80 | 0 | my ($indent, $rest) = ($1, $2); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( length($indent)/2 == $stack_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # indents have two spaces, that's why length is over 2 | ||||||||||||||||||||||||||||
| 83 | 0 | my $instance_name = "\l$type$objects{$key}"; | |||||||||||||||||||||||||||
| 84 | 0 | $output[$i] = "$indent$instance_name:$rest\n"; | |||||||||||||||||||||||||||
| 85 | 0 | last; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | $ret; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { # void context | ||||||||||||||||||||||||||||
| 92 | 0 | &$DB::sub; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub DB { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my ($pack) = caller(0); | ||||||||||||||||||||||||||
|
Subroutine
DB: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return if ($pack =~ /Dumper/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | my $frame = DB->new(); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unless ($frame->compare($previous_frame)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $old_depth = $previous_frame->depth() if ref $previous_frame; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | my $new_depth = $frame ->depth(); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (not defined $old_depth or $new_depth > $old_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $old_depth or $new_depth > $old_depth
MC/DC0%0/2
not defined $old_depth or $new_depth > $old_depth
not defined $old_depth
$new_depth > $old_depth
Truth table0%0/3
not(A) or B
A= not defined $old_depthB= $new_depth > $old_depth
| |||||||||||||||||||||||||||||
| 106 | 0 | my $output = " " x ($new_depth - 1); | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (defined $frame->element(1)) { # if someone called, say so | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | my $arg = $frame->arg(1); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $arg =~ /([^=]*)=(.*)/; | |||||||||||||||||||||||||||
| 111 | 0 | my ($type, $key) = ($1, $2); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (defined $key and defined $objects{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key and defined $objects{$key}
MC/DC0%0/2
defined $key and defined $objects{$key}
defined $key
defined $objects{$key}
Truth table0%0/3
A and B
A= defined $keyB= defined $objects{$key}
| |||||||||||||||||||||||||||||
| 113 | 0 | $output .= "\l$type$objects{$key}:"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | $output .= $frame->subname(1); # . " " . $frame->arg(1); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { # first call only, it has no parent | ||||||||||||||||||||||||||||
| 119 | 0 | $output .= $frame->package(0); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | $output .= "\n"; | |||||||||||||||||||||||||||
| 122 | 0 | push @output, $output; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | $previous_frame = $frame; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # I can't use the following package statement, caller doesn't set DB::args | ||||||||||||||||||||||||||||
| 130 | # unless it's called from the DB package. | ||||||||||||||||||||||||||||
| 131 | # package StackFrame; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub new { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $i = 1; | |||||||||||||||||||||||||||
| 136 | 0 | my $frame = []; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | while (my ($package, $file, $line, $subname) = caller($i++)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | my $arg = $DB::args[-1]; | |||||||||||||||||||||||||||
| 140 | 0 | push @$frame, { pack => $package, | |||||||||||||||||||||||||||
| 141 | file => $file, | ||||||||||||||||||||||||||||
| 142 | subname => $subname, | ||||||||||||||||||||||||||||
| 143 | arg => $arg, | ||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | return bless $frame, $class; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub compare { | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
compare: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $other = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return 0 unless (ref $self and ref $other); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self and ref $other
MC/DC0%0/2
ref $self and ref $other
ref $self
ref $other
Truth table0%0/3
A and B
A= ref $selfB= ref $other
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return 0 if (@$self != @$other); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | foreach my $element (1 .. @$self) { | |||||||||||||||||||||||||||
| 156 | 0 | my $self_element = $self ->[$element]; | |||||||||||||||||||||||||||
| 157 | 0 | my $other_element = $other->[$element]; | |||||||||||||||||||||||||||
| 158 | no warnings; | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return 0 if ($self_element->{pack} ne $other_element->{pack}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return 0 if ($self_element->{file} ne $other_element->{file}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | return 0 if ($self_element->{subname} ne $other_element->{subname}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | return 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub depth { | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
depth: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return 0 unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | return @$self; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub element { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
element: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 175 | 0 | my $number = shift; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return undef unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if (defined $self->[$number]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | return $self->[$number]; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||
| 182 | 0 | return undef; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub package { | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
package: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | my $element = shift; | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | return undef unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | return $self->[$element]{pack}; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub subname { | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
subname: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | 0 | my $element = shift; | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | return undef unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | return $self->[$element]{subname}; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub arg { | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
arg: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 204 | 0 | my $element = shift; | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | return undef unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | return $self->[$element]{arg}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1; | ||||||||||||||||||||||||||||