Quiq/Stacktrace.pm

filter: Statementstmt 92.8% 26 / 28 Branchbran 87.5% 7 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 84.6% 44 / 52 CC 9 SCAR 16.5
CC
9
Cov
86%
asString
8.0
new
3.0
BEGIN
1.0
CRAP 5.2
« Quiq/JQuery/Form/ViewEdit.pm ↑ Summary Quiq/ChartJs/TimeSeries.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Stacktrace - Generiere und visualisiere einen Stacktrace
8  
9 =head1 SYNOPSIS
10  
11 use Quiq::Stacktrace;
12
13 my $st = Quiq::Stacktrace->new; # generiere Stacktrace
14 print $st->asString,"\n"; # visualisiere Stacktrace
15
16 -or-
17
18 print Quiq::Stacktrace->asString,"\n"; # in einem Aufruf
19  
20 =head1 DESCRIPTION
21  
22 Ein Objekt der Klasse repräsentiert die Aufrufhierarchie des
23 laufenden Perl-Programms zum Zeitpunkt der Instantiierung des
24 Stacktrace-Objektes.
25  
26 Die Klasse kann zum Debuggen verwendet werden oder bei der
27 Erzeugung von Exceptions.
28  
29 Folgendes Beispielprogramm (test.pl)
30  
31 #!/usr/bin/env perl
32
33 use Quiq::Stacktrace;
34
35 sub a {
36 b();
37 }
38
39 sub b {
40 c();
41 }
42
43 sub c {
44 print Quiq::Stacktrace->asString,"\n";
45 }
46
47 a();
48  
49 erzeugt die Ausgabe
50  
51 main::a() [+17 ./test.pl]
52 main::b() [+6 ./test.pl]
53 main::c() [+10 ./test.pl]
54 Quiq::Stacktrace::asString() [+14 ./test.pl]
55  
56 Von oben nach unten gelesen gibt der Stacktrace die Hierarchie der
57 Subroutine-Aufrufe (= Methoden oder Funktionsaufrufe) in der
58 Aufrufreihenfolge wieder. Jede Zeile beschreibt einen Subroutine-Aufruf:
59  
60 main::a() [+17 ./test.pl]
61 ^ ^ ^ ^
62 | | | +-- Datei, in der der Aufruf steht
63 | | +-- Zeilennummer, an der der Aufruf in der Datei steht
64 | +-- Name der aufgerufenen Subroutine
65 +-- Package, zu dem die Subroutine gehört
66  
67 Die in eckigen Klammern genannte Quelltextposition kann bei
68 Aufruf von vi(1), emacs(1) oder less(1) benutzt werden,
69 um unmittelbar auf die entsprechende Zeile zu positionieren:
70  
71 $ less +17 ./test.pl
72  
73 Der unterste Eintrag im Stacktrace ist der Aufruf des
74 Konstruktors L<new|"new() - Konstruktor">() oder der Methode L<asString|"asString() - Visualisiere Stacktrace-Objekt">(), wenn sie
75 als Klassenmethode gerufen wird. Sollen Stacktrace-Frames am
76 Ende weggelassen werden, kann dies durch Angabe des Parameters
77 $i erreicht werden.
78  
79 =cut
80  
81 # -----------------------------------------------------------------------------
82  
83 package Quiq::Stacktrace;
84  
85 271 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 271 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 271 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 271 our $VERSION = '1.240';
90  
91 # -----------------------------------------------------------------------------
92  
93 =head1 METHODS
94  
95 =head2 Instantiierung
96  
97 =head3 new() - Konstruktor
98  
99 =head4 Synopsis
100  
101 $st = $class->new;
102 $st = $class->new($i);
103  
104 =head4 Arguments
105  
106 =over 4
107  
108 =item $i (Default: 0)
109  
110 Anzahl der Stackframes vom Ende des Stacktrace, die nicht
111 berücksichtigt werden. Ist $i == 0 (was der Default ist), werden
112 alle Frames berücksichtigt.
113  
114 =back
115  
116 =head4 Description
117  
118 Instantiiere ein Stacktrace-Objekt und liefere eine Referenz auf diese
119 Objekt zurück. Das Stacktrace-Objekt repräsentiert die Aufruf-Hierarchie
120 des laufenden Perl-Programms zum Zeitpunkt der Instantiierung. Letztes
121 Element in der Hierarchie ist der Konstruktor-Aufruf. Soll der
122 Stacktrace vorher enden, wird der Parameter $i gesetzt.
123  
124 =cut
125  
126 # -----------------------------------------------------------------------------
127  
128 152 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
129 152 my $class = shift;
130 152 ▶ my $i = shift || 0;
Condition100%2/2
shift() || 0
l!l
1502
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
131  
132 152 my @frames;
133 152 ▶ while (my (undef,$file,$line,$sub) = caller $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $file, $line, $sub) = caller $i++) 1479 152 1631
134 1479 push @frames,[$file,$line,$sub];
135 }
136  
137 152 return bless \@frames,$class;
138 }
139  
140 # -----------------------------------------------------------------------------
141  
142 =head2 Externe Repräsentation
143  
144 =head3 asString() - Visualisiere Stacktrace-Objekt
145  
146 =head4 Synopsis
147  
148 $str = $st->asString;
149 $str = $class->asString($i);
150  
151 =head4 Arguments
152  
153 =over 4
154  
155 =item $i (Default: 0)
156  
157 Siehe L<new|"new() - Konstruktor">().
158  
159 =back
160  
161 =head4 Description
162  
163 Visualisiere das Stacktrace-Objekt in Form einer Zeichenkette und liefere
164 diese zurück. Aufbau der Zeichenkette siehe Abschnitt DESCRIPTION.
165  
166 =cut
167  
168 # -----------------------------------------------------------------------------
169  
170 152 ▶sub asString {
Subroutine asString: called CC=7 SCAR=20.8
Pod documented
171 152 ▶ my $self = ref $_[0]? $_[0]: $_[0]->new(($_[1] || 0)+1);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 150 152
Condition50%1/2
$_[1] || 0
l!l
0150
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
172  
173 152 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 152 152
174 0 ▶ my $i = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
175 0 $self = $self->new($i);
176 }
177  
178 152 my $str = '';
179  
180 152 my $i = 0;
181 152 for my $frame (reverse @$self) {
182 1479 my ($file,$line,$sub) = @$frame;
183 1479 ▶ $sub .= "()" if $sub ne '(eval)';
Branch: 1 branch
BranchTrueFalseTotal
if $sub ne "(eval)" 1097 382 1479
184 1479 $str .= sprintf "%s%s [+%s %s]\n",(' 'x$i++),$sub,$line,$file;
185 }
186 152 chomp $str;
187  
188 152 return $str;
189 }
190  
191 # -----------------------------------------------------------------------------
192  
193 =head1 VERSION
194  
195 1.240
196  
197 =head1 AUTHOR
198  
199 Frank Seitz, L<http://fseitz.de/>
200  
201 =head1 COPYRIGHT
202  
203 Copyright (C) 2026 Frank Seitz
204  
205 =head1 LICENSE
206  
207 This code is free software; you can redistribute it and/or modify
208 it under the same terms as Perl itself.
209  
210 =cut
211  
212 # -----------------------------------------------------------------------------
213  
214 271 1;
215  
216 # eof
« Quiq/JQuery/Form/ViewEdit.pm ↑ Summary Quiq/ChartJs/TimeSeries.pm »