blib/lib/CGI/WebOut.pm

filter: Statementstmt 52.1% 149 / 286 Branchbran 29.3% 37 / 126 Conditioncond 42.8% 9 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 53.5% 30 / 56 Podpod 56.0% 14 / 25 totaltotal 45.4% 240 / 528 CC 72 SCAR 33.2
CC
72
Cov
45%
__PrintAllErrors
182.0
NoCache
42.0
Flush
34.3
CRAP 27.6
↑ Summary
1 #
2 # Ãàðàíòèðóåò, ÷òî âûâîä ÷åðåç print ìîæíî áåçîïàñíî íàïðàâèòü â áðàóçåð.
3 # Òî åñòü, áóôåðèçóåò åãî, ñëåäèò çà çàãîëîâêàìè, à òàêæå çà òåì, ÷òîáû
4 # çàãîëîâîê Content-type âñåãäà âûâîäèëñÿ ïåðåä òåêñòîì äîêóìåíòà.
5 # Ïîìèìî âñåãî ýòîãî, ñëåäèò çà îøèáêàìè, âîçíèêàþùèìè â ñêðèïòå, è
6 # ïåðåíàïðàâëÿåò èõ â áðàóçåð (â âèäå êîììåíòàðèåâ èëè âèäèìîãî òåêñòà).
7 # Òàêæå ïîçâîëÿåò âûáîðî÷íî ïåðåõâàòûâàòü âûõîäíîé ïîòîê ñêðèïòà äëÿ
8 # ïîñëåäóþùåé îáðàáîòêè: $text = grab { print "Hello" }.
9 # Â îáùåì, ïîëíàÿ ýìóëÿöèÿ ïîâåäåíèÿ PHP.
10 package CGI::WebOut;
11 1 our $VERSION = "2.25";
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Exporter; our @ISA=qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our @EXPORT=qw(
16 ER_NoErr
17 ER_Err2Browser
18 ER_Err2Comment
19 ER_Err2Plain
20 ErrorReporting
21 grab
22 echo
23 SetAutoflush
24 NoAutoflush
25 UseAutoflush
26 Header
27 HeadersSent
28 Redirect
29 ExternRedirect
30 NoCache
31 Flush
32 try catch warnings throw
33 );
34  
35  
36 ##
37 ## Êîíñòàíòû
38 ##
39 0 ▶sub ER_NoErr { 0 } # Çàïðåòèòü âûâîä îá îøèáêàõ
Subroutine ER_NoErr: not called CC=1 SCAR=6.9
Pod undocumented
40 0 ▶sub ER_Err2Browser { 1 } # Îøèáêè è ïðåäóïðåæäåíèÿ íàïðàâèòü â áðàóçåð
Subroutine ER_Err2Browser: not called CC=1 SCAR=6.9
Pod undocumented
41 0 ▶sub ER_Err2Comment { 2 } # Òî æå, íî â âèäå <!--...-->-êîììåíòàðèåâ
Subroutine ER_Err2Comment: not called CC=1 SCAR=6.9
Pod undocumented
42 0 ▶sub ER_Err2Plain { 3 } # Òî æå, íî â âèäå plain-òåêñòà
Subroutine ER_Err2Plain: not called CC=1 SCAR=6.9
Pod undocumented
43  
44  
45 ##
46 ## Âíóòðåííèå ïåðåìåííûå
47 ##
48 #our $DEBUG = "/wo"; # îòëàäî÷íûé ðåæèì - çàäàåò èìÿ ôàéëà.
49 1 our $DEBUG = undef; # îòëàäî÷íûé ðåæèì - çàäàåò èìÿ ôàéëà.
50 1 our $UseAutoflush = 1; # Ðåæèì àâòîñáðîñà âêëþ÷åí
51 1 our $HeadersSent = 0; # ïðèçíàê: çàãîëîâêè óæå ïîñëàíû
52 1 our @Headers = (); # çàãîëîâêè îòâåòà
53 1 our $NoCached = 0; # äîêóìåíò íå êýøèðóåòñÿ
54 1 our $Redirected = 0; # áûëà ïåðåàäðåñàöèÿ
55 1 our $ErrorReporting = 1; # âûâîä îøèáîê â áðàóçåð âêëþ÷åí
56 1 our @Errors = (); # çäåñü íàêàïëèâàþòñÿ îøèáêè
57 1 our @Warns = (); # ïðåäóïðåæäåíèÿ
58  
59 # Ê ñîæàëåíèþ, âìåñòî òîãî, ÷òîáû õðàíèòü òåêóùèé è êîðíåâîé ÎÁÚÅÊÒÛ âûâîäà,
60 # ïðèõîäèòñÿ õðàíèòü ëèøü ÑÒÐÎÊÎÂÛÅ ÁÓÔÅÐÀ. Äåëî â òîì, ÷òî õðàíåíèå îáúåêòà
61 # óâåëè÷èâàåò åãî ñ÷åò÷èê ññûëîê, à çíà÷èò, åñëè â ïðîãðàììå âñòðåòèòñÿ:
62 # $b = new CGI::WebOut();
63 # ...
64 # $b = undef;
65 # òî äåñòðóêòîð äëÿ $b âûçâàí íå áóäåò (ò.ê. ññûëêà íà íåãî çàïèñàíà â $CurObj).
66 # Åñëè æå õðàíèòü â îáúåêòàõ ññûëêè íà ñòðîêîâûå áóôåðà, à ññûëêè íà ñàìè
67 # îáúåêòû äîïîëíèòåëüíî ÍÅ õðàíèòü, äåñòðóêòîð âûçûâàåòñÿ, êàê íàäî.
68 #
69 # Ýòî âñå òàêæå îçíà÷àåò, ÷òî ïîëó÷èòü â ýòîì ìîäóëå ññûëêó íà òåêóùèé
70 # ÎÁÚÅÊÒ ÂÛÂÎÄÀ íåëüçÿ íèêàê. Ìîæíî ëèøü ïîëó÷èòü ññûëêó íà åãî áóôåð.
71 # Òàêèì îáðàçîì, echo âñåãäà ðàáîòàåò ñî ñòðîêîâûì áóôåðîì, íî ÍÅ îáúåêòîì.
72 1 our $rRootBuf = \(my $s=""); # ãëàâíûé áóôåð âûâîäà
73 1 our $rCurBuf; # òåêóùèé áóôåð âûâîäà
74  
75 #
76 # Algorythm is:
77 # 1. Tie STDOUT to newly created CGI::WebOut::Tie.
78 # 2. Constructor CGI::WebOut::Tie->RIEHANDLE creates new objecc CGI::WebOut
79 # and stores its reference in its property. It is IMPORTANT that there
80 # are NO other reserences to this object stored in some other place.
81 # 3. That's why, when STDOUT is untied (in END or during global destruction)
82 # CGI::WebOut object is destroyed too.
83 # 4. In destructor CGI::WebOut->DESTROY works code: if this object is the
84 # first (root), Flush() is called and errors are printed.
85 #
86  
87  
88 # Synopsis: use CGI::WebOut($restart=0)
89 # Ïðè ïîäêëþ÷åíèè ïðîâåðÿåò ñâÿçûâàíèå STDOUT è, åñëè ñâÿùàííîñòè íåò èëè
90 # îíà ïîìåíÿëàñü, óñòàíàâëèâàåò åå íà ñåáÿ.
91 #
92 # Âíèìàíèþ ïîëüçîâàòåëåé FastCGI: import ðàáîòàåò íå òàê, êàê õîòåëîñü áû.
93 # Íàïðèìåð, åñëè â öèêëå ñ äâóìÿ èòåðàöèÿìè íàïèñàòü use CGI::WebOut, òî
94 # ðåàëüíî import áóäåò âûçâàí òîëüêî 1 ðàç. Çàòî åñëè äâàæäû íàïèñàòü ýòó æå
95 # êîìàíäó, òî import âûçîâåòñÿ äâàæäû. Ãàðàíòèðîâàííî import çàïóñêàåòñÿ
96 # ñëåäóþùåé êîíñòðóêöèåé: eval("use CGI::WebOut(1)"). Åå ðåêîìåíäóåòñÿ
97 # âñòàâëÿòü âíóòðü öèêëà îáðàáîòêè ïîäêëþ÷åíèé FastCGI.
98 #
99 # Åñëè ïàðàìåòð $restart ðàâåí true, òî âñå ïðîèñõîäèò òàê, áóäòî áû
100 # çàãîëîâêè îòâåòà åùå íèêîãäà íå ïîñûëàëèñü.
101  
102  
103 ##
104 ## Îáùåäîñòóïíûå ñòàòè÷åñêèå ôóíêöèè.
105 ##
106  
107 # void retieSTDOUT($restart=false)
108 # Çàïîìèíàåì ñòàðûé STDOUT (äëèííîå íàçâàíèå - ñïåöèàëüíî, ÷òîáû íå
109 # çëîóïîòðåáëÿëè!) è óñòàíàâëèâàåò ñâîé ïåðåõâàò÷èê íà STDOUT.  ñëó÷àå,
110 # åñëè ñâîé ïåðåõâàò÷èê óæå óñòàíîâëåí, íè÷åãî íå äåëàåò.
111 1 my $numReties;
112 sub retieSTDOUT
113 2 ▶{ my ($needRestart) = @_;
Subroutine retieSTDOUT: called CC=5 SCAR=17.5
Pod undocumented
114 2 ▶ $needRestart ||= !$numReties++;
Condition66%2/3
$needRestart ||= !$numReties++
l!l&&r!l&&!r
110
MC/DC0%0/2
$needRestart ||= !$numReties++
$needRestart !$numReties++
Truth table66%2/3
A ||= not(B)
A= $needRestartB= !$numReties++
ABresult
1X1
011
000
115 # Handle all warnings and errors.
116 0 ▶ $SIG{__WARN__} = sub { &Warning(($_[0] !~ /^\w+:/ ? "Warning: " : "").shift) };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
!($_[0] =~ /^\w+:/) ? : 0 0 0
117 2 ▶ $SIG{__DIE__} = sub { return if ref $_[0]; &Warning(($_[0] !~ /^\w+:/ ? "Fatal: " : "").shift) };
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] 0 1 1
!($_[0] =~ /^\w+:/) ? : 1 0 1
118 # Åñëè íà÷àò íîâûé ñêðèïò, ñáðàñûâàåì ïðèçíàê îòñûëêè çàãîëîâêîâ.
119 2 ▶ if ($needRestart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needRestart) 2 0 2
120 2 $HeadersSent = $Redirected = $NoCached = 0;
121 2 @Headers = ();
122 2 $$rRootBuf = '';
123 }
124 # Åñëè íè÷åãî íå èçìåíèëîñü, âûõîäèì
125 2 ▶ return if tied(*STDOUT) && ref tied(*STDOUT) eq __PACKAGE__."::Tie";
Branch: 1 branch
BranchTrueFalseTotal
if tied *STDOUT and ref tied *STDOUT eq "CGI::WebOut::Tie" 0 2 2
Condition66%2/3
tied *STDOUT and ref tied *STDOUT eq "CGI::WebOut::Tie"
!ll&&!rl&&r
110
MC/DC0%0/2
tied *STDOUT and ref tied *STDOUT eq "CGI::WebOut::Tie"
tied *STDOUT ref tied *STDOUT eq "CGI::WebOut::Tie"
Truth table66%2/3
A and B
A= tied *STDOUTB= ref tied *STDOUT eq "CGI::WebOut::Tie"
ABresult
0X0
100
111
126 2 tie(*STDOUT, __PACKAGE__."::Tie", \*STDOUT, tied(*STDOUT));
127 }
128  
129  
130 # Ïðîâåðÿåò, èñïîëüçóåòñÿ ëè áèáëèîòåêà Web-ñêðèïòîì èëè îáû÷íûì
131 16 ▶sub IsWebMode() {
Subroutine IsWebMode: called CC=2 SCAR=7.6
Pod undocumented
132 16 ▶ return $ENV{SCRIPT_NAME}? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SCRIPT_NAME'} ? : 0 16 16
133 }
134  
135  
136 # Ïîñëàíû ëè çàãîëîâêè?
137 0 ▶sub HeadersSent {
Subroutine HeadersSent: not called CC=1 SCAR=6.9
Pod undocumented
138 0 return $HeadersSent;
139 }
140  
141  
142 # static int echo(...)
143 # Âûâîäèò ñïèñîê àãðóìåíòîâ â ÒÅÊÓÙÈÉ àêòèâíûé áóôåð. Åñëè ýòîò
144 # áóôåð íàïðàâëåí íåïîñðåäñòâåííî â áðàóçåð, âûçûâàåò Flush().
145 # Âîçâðàùàåò ÷èñëî âûâåäåííûõ ñèìâîëîâ.
146 19 ▶sub echo {
Subroutine echo: called CC=7 SCAR=22.0
Pod undocumented
147 #  ñëó÷àå íàëè÷èÿ undef-çíà÷åíèé â ñïèñêå äåëàåì òî æå,
148 # ÷òî è print.
149 19 ▶ if(grep {!defined $_} @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} @_) 0 19 19
150 # Åñëè ìîäóëÿ íåò - íå ñòðàøíî, ïðîñòî íè÷åãî íå ïå÷àòàåòñÿ.
151 0 ▶ eval { require Carp }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Carp } } and Carp::carp("Use of uninitialized value in print") 0 0 0
152 and Carp::carp("Use of uninitialized value in print");
153 }
154 19 ▶ my $txt = join("", map { defined $_? $_:"" } @_);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 19 0 19
155 19 ▶ return if $txt eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $txt eq "" 0 19 19
156 19 $$rCurBuf .= $txt;
157 19 ▶ Flush() if $UseAutoflush && $rCurBuf == $rRootBuf;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::WebOut::UseAutoflush and $CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf 14 5 19
Condition66%2/3
$CGI::WebOut::UseAutoflush and $CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf
!ll&&!rl&&r
0514
MC/DC50%1/2
$CGI::WebOut::UseAutoflush and $CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf
$CGI::WebOut::UseAutoflush $CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf
Truth table66%2/3
A and B
A= $CGI::WebOut::UseAutoflushB= $CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf
ABresult
0X0
100
111
158 19 return length($txt);
159 }
160  
161  
162 # Ïåðåõâàò âûõîäíîãî ïîòîêà. Èñïîëüçîâàíèå:
163 # $grabbed = grab {
164 # print 'Hello!'
165 # } catch {
166 # die "An error occurred while grabbing the output: $@";
167 # };
168 # èëè òî æå, íî áåç catch:
169 # $grabbed = grab { print 'Hello!' };
170 sub grab(&@)
171 3 ▶{ my ($func, $catch)=@_;
Subroutine grab: called CC=3 SCAR=12.7
Pod documented
172 3 my $Buf = CGI::WebOut->new;
173 3 $@ = undef; eval { &$func() };
174 0 ▶ if ($@ && $catch) { chomp($@); local $_ = $@; &$catch; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and $catch) 0 3 3
Condition33%1/3
$@ and $catch
!ll&&!rl&&r
300
MC/DC0%0/2
$@ and $catch
$@ $catch
Truth table33%1/3
A and B
A= $@B= $catch
ABresult
0X0
100
111
175 3 return $Buf->buf;
176 }
177  
178  
179 # static Header($header)
180 # Óñòàíàâëèâàåò çàãîëîâîê îòâåòà.
181 sub Header($)
182 0 ▶{ my ($head)=@_;
Subroutine Header: not called CC=3 SCAR=24.8
Pod documented
183 0 ▶ if ($HeadersSent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::WebOut::HeadersSent) 0 0 0
184 0 ▶ eval { require Carp }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Carp } } and Carp::carp("Oops... Header('${head}') called after content had been sent to browser!\n") 0 0 0
185 and Carp::carp("Oops... Header('$head') called after content had been sent to browser!\n");
186 0 return undef;
187 }
188 0 push(@Headers, $head);
189 0 return 1;
190 }
191  
192  
193 # Ñáðàñûâàåò ñîäåðæèìîå ãëàâíîãî áóôåðà â áðàóçåð.
194 16 ▶sub Flush() {
Subroutine Flush: called CC=8 SCAR=35.4
Pod documented
195 # Îòêëþ÷àåì âíóòðåííþþ áóôåðèçàöèþ Perl-à
196 16 local $| = 1;
197 # Åñëè çàãîëîâêè åùå íå îòîñëàíû, îòîñëàòü èõ
198 16 ▶ if (!$HeadersSent && IsWebMode()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $CGI::WebOut::HeadersSent and &IsWebMode()) 0 16 16
Condition33%1/3
not $CGI::WebOut::HeadersSent and &IsWebMode()
!ll&&!rl&&r
0160
MC/DC0%0/2
not $CGI::WebOut::HeadersSent and &IsWebMode()
not $CGI::WebOut::HeadersSent &IsWebMode()
Truth table33%1/3
not(A) and B
A= not $CGI::WebOut::HeadersSentB= &IsWebMode()
ABresult
0X0
100
111
199 0 my $ContType="text/html";
200 0 unshift(@Headers,"X-Powered-By: CGI::WebOut v$VERSION (http://www.dklab.ru/chicken/4.html), (C) by Dmitry Koterov");
201 # Èùåì Content-type, ÷òîáû ïîòîì îòïðàâèòü åãî â êîíöå
202 0 ▶ for (my $i=0; $i<@Headers; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @CGI::WebOut::Headers) 0 0 0
203 0 ▶ if ($Headers[$i]=~/^content-type: *(.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::WebOut::Headers[$i] =~ /^content-type: *(.*)$/i) 0 0 0
204 0 $ContType = $1; splice(@Headers, $i, 1); $i--;
205 0 next;
206 }
207 0 ▶ if ($Headers[$i]=~m/^location: /i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::WebOut::Headers[$i] =~ /^location: /i) 0 0 0
208 0 $Redirected = 1;
209 }
210 }
211 0 ▶ if (!$Redirected) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $CGI::WebOut::Redirected) { } 0 0 0
212 0 push(@Headers, "Content-type: $ContType");
213 0 my $headers = join("\n",@Headers)."\n\n";
214 # Prepend the output buffer with headers data.
215 # So we output the buffer and headers in ONE print call (it is
216 # more transparent for calling code if it ties STDOUT by himself).
217 0 $$rRootBuf = $headers.$$rRootBuf;
218 } else {
219 # Only headers should be sent.
220 0 my $headers = join("\n",@Headers)."\n\n";
221 0 _RealPrint($headers);
222 }
223 0 $HeadersSent = 1;
224 }
225 # Îòïðàâèòü áóôåð è î÷èñòèòü åãî
226 16 _Debug("Flush: len=%d", length($$rRootBuf));
227 16 ▶ if (!$Redirected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CGI::WebOut::Redirected) 16 0 16
228 16 _RealPrint($$rRootBuf);
229 }
230 16 $$rRootBuf = "";
231 16 return 1;
232 }
233  
234  
235 # constructor new($refToNewBuf=undef)
236 # Äåëàåò òåêóùèì íîâûé áóôåð âûâîäà.
237 sub new
238 5 ▶{ my ($class, $rBuf)=@_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
239 5 ▶ $rBuf = \(my $b="") if !defined $rBuf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rBuf 3 2 5
240 5 my $this = bless {
241 rPrevBuf => $rCurBuf,
242 rCurBuf => $rBuf,
243 }, $class;
244 5 $rCurBuf = $rBuf;
245 5 _Debug("[%s] New: prevSt=%s, curSt=%s", $this, $this->{rPrevBuf}, $this->{rCurBuf});
246 5 return $this;
247 }
248  
249  
250 # Âîññòàíàâëèâàåò ïðåäûäóùèé àêòèâíûé îáúåêò âûâîäà
251 sub DESTROY
252 5 ▶{ my ($this)=@_;
Subroutine DESTROY: called CC=3 SCAR=11.0
253 5 _Debug("[%s] DESTROY: prevSt=%s, curSt=%s", $this, $this->{rPrevBuf}, $this->{rCurBuf});
254  
255 # Åñëè ýòî ïîñëåäíèé îáúåêò, òî âûïîëíÿåì äåéñòâèÿ, êîòîðûå íóæíî îáÿçàòåëüíî
256 # çàêîí÷èòü ê ìîìåíòó çàâåðøåíèÿ ïðîãðàììû. Òî åñòü, ýòîò ó÷àñòîê êîäà âûïîëíÿåòñÿ
257 # òîãäà è òîëüêî òîãäà, êîãäà âûçûâàåòñÿ DESTROY äëÿ îáúåêòà, ñâÿçàííîãî ñ
258 # STDOUT, òî åñòü ïåðåä ñàìûì çàâåðøåíèåì ïðîãðàììû (ïî îøèáêå èëè íåò - íå âàæíî).
259 # Âñå ýòè ñëîæíîñòè íóæíû òîëüêî ïîòîìó, ÷òî, îêàçûâàåòñÿ, â Perl íåëüçÿ
260 # îáúÿâèòü ôóíêöèþ, êîòîðàÿ áóäåò ãàðàíòèðîâàíî âûçûâàòüñÿ â êîíöå, îñîáåííî ïðè
261 # ôàòàëüíîé îøèáêå... Îäíàêî ìîæíî ñîçäàòü íåêîòîðûé îáúåêò, êîòîðûé ïðè óíè÷òîæåíèè
262 # âûçîâåò ñâîé äåñòðóêòîð. Òàêèì îáúåêòîì äëÿ íàñ áóäåò îáúåêò, ñâÿçàííûé
263 # ñ STDOUT. Íàì ýòî æèçíåííî íåîáõîäèìî, ïîòîìó ÷òî íóæíî ëþáîé öåíîé âûâåñòè
264 # çàãîëîâêè è, âîçìîæíî, ñîîáùåíèÿ î âîçíèêøèõ îøèáêàõ. Ýòî, ñîáñòâåííî, è
265 # äåëàåòñÿ çäåñü.
266 5 ▶ if ($rCurBuf == $rRootBuf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::WebOut::rCurBuf == $CGI::WebOut::rRootBuf) 2 3 5
267 # Âûçûâàåìàÿ îòñþäà ôóíêöèÿ ÍÅ ÌÎÆÅÒ èñïîëüçîâàòü print è STDOUT, ïîòîìó ÷òî
268 # â ìîìåíò ïðîõîæäåíèÿ ýòîé òî÷êè STDOUT íè ê ÷åìó íå "ïðèâÿçàí", íî
269 # Perl-ó êàæåòñÿ, ÷òî ïðèâÿçàí, ïîýòîìó ãåíåðèðóåòñÿ GP Fault.
270 2 ▶ &__PrintAllErrors() if @Errors;
Branch: 1 branch
BranchTrueFalseTotal
if @CGI::WebOut::Errors 0 2 2
271 2 Flush();
272 2 return;
273 }
274 3 $rCurBuf = $this->{rPrevBuf};
275 }
276  
277  
278 # string method buf()
279 # Âûçûâàåòñÿ äëÿ ïîëó÷åíèÿ äàííûõ èç áóôåðà âûâîäà.
280 3 ▶sub buf {
Subroutine buf: called CC=1 SCAR=0.0
Pod undocumented
281 3 return ${$_[0]->{rCurBuf}};
282 }
283  
284  
285  
286 ##
287 ## Ñëóæåáíûå ôóíêöèè è ìåòîäû.
288 ##
289  
290 # constructor _newRoot()
291 # Creates the new ROOT (!!!) buffer. Called internally while tying STDOUT.
292 2 ▶sub _newRoot {
Subroutine _newRoot: called CC=1 SCAR=0.0
293 2 $$rRootBuf = "";
294 2 $rCurBuf = undef;
295 2 goto &new;
296 }
297  
298  
299 # Package import.
300 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
301 2 my ($pkg, $needRestart)=@_;
302 2 retieSTDOUT($needRestart);
303 2 goto &Exporter::import;
304 }
305  
306  
307 # Äåñòðóêòîð ïàêåòà. Ñëåäèò çà òåì, ÷òîáû âñå îáúåêòû áûëè óäàëåíû â
308 # ïðàâèëüíîì ïîðÿäêå. Âûçûâàåòñÿ ÄÎ ôàçû "global destruction", ÷òî
309 # íàì è íóæíî. Ïðàâäà, åñòü ñâåäåíèÿ, ÷òî èíîãäà END ÍÅ âûçûâàåòñÿ
310 # (â ñëó÷àå êàêèõ-òî îøèáîê), îäíàêî è â ýòîì ñëó÷àå âñå áóäåò ðàáîòàòü
311 # êîððåêòíî (ñì. _RealPrint).
312 1 ▶sub END {
Subroutine END: called CC=3 SCAR=18.1
313 1 ▶ return if !tied(*STDOUT) || ref tied(*STDOUT) ne __PACKAGE__."::Tie";
Branch: 1 branch
BranchTrueFalseTotal
if not tied *STDOUT or ref tied *STDOUT ne "CGI::WebOut::Tie" 1 0 1
Condition33%1/3
not tied *STDOUT or ref tied *STDOUT ne "CGI::WebOut::Tie"
l!l&&r!l&&!r
100
MC/DC0%0/2
not tied *STDOUT or ref tied *STDOUT ne "CGI::WebOut::Tie"
not tied *STDOUT ref tied *STDOUT ne "CGI::WebOut::Tie"
Truth table33%1/3
not(A) or B
A= not tied *STDOUTB= ref tied *STDOUT ne "CGI::WebOut::Tie"
ABresult
1X1
011
000
314 0 CGI::WebOut::_Debug("CGI::WebOut::END");
315 0 my $this = tied(*STDOUT);
316 0 my ($handle, $obj) = ($this->{handle}, $this->{prevObj});
317 0 CGI::WebOut::Tie::tieobj(*$handle, $obj)
318 }
319  
320  
321 # static _RealPrint()
322 # Prints the data to "native" STDOUT handler.
323 16 ▶sub _RealPrint {
Subroutine _RealPrint: called CC=4 SCAR=15.0
324 16 my $obj = tied(*STDOUT);
325 16 _Debug("_RealPrint: STDOUT tied: %s", $obj);
326 16 my $txt = join("", @_);
327 16 ▶ return if $txt eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $txt eq "" 2 14 16
328 14 ▶ if ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) { } 14 0 14
329 14 ▶ if (ref $obj eq "CGI::WebOut::Tie") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq 'CGI::WebOut::Tie') { } 14 0 14
330 14 return $obj->parentPrint(@_)
331 } else {
332 0 print STDOUT @_;
333 }
334 } else {
335 # Sometimes, during global destruction, STDOUT is already untied
336 # but print still does not work. I don't know, why. This workaround
337 # works always.
338 0 open(local *H, ">&STDOUT");
339 0 return print H @_;
340 }
341 }
342  
343  
344 # Äëÿ îòëàäêè - âûâîäèò ñîîáùåíèå â ôàéë
345 my $opened;
346 75 ▶sub _Debug {
Subroutine _Debug: called CC=6 SCAR=35.3
347 75 ▶ return if !$DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $CGI::WebOut::DEBUG 75 0 75
348 0 my ($msg, @args) = @_;
349  
350 # Detect "global destruction" stage.
351 0 my $gd = '';
352 {
353 0 ▶ local $SIG{__WARN__} = sub { $gd .= $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 warn("test");
355 0 $gd =~ s/^.*? at \s+ .*? \s+ line \s+ \d+ \s+//sx;
356 0 $gd =~ s/^\s+|[\s.]+$//sg;
357 0 ▶ $gd = undef if $gd !~ /global\s*destruction/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $gd =~ /global\s*destruction/i 0 0 0
358 }
359 0 local $^W;
360 0 ▶ open(local *F, ($opened++? ">>" : ">").$DEBUG); binmode(F);
Branch: 1 branch
BranchTrueFalseTotal
$opened++ ? : 0 0 0
361 0 ▶ print F sprintf($msg, map { defined $_? $_ : "undef" } @args) . ($gd? " ($gd)" : "")."\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 0 0 0
$gd ? : 0 0 0
362 }
363  
364  
365 ##
366 ## Äëÿ ïåðåõâàòà âûâîäà print-à
367 ##
368  
369 1 {{{
370 ##
371 ## This class is used to tie some Perl variable to specified $object
372 ## WITHOUT calling TIE* method of ref($object). Unfortunately Perl
373 ## does not support
374 ## tied(thing) = something;
375 ## construction. Instead of this use:
376 ## tie(thing, "CGI::WebOut::TieMediator", something).
377 ## See tieobj() below.
378 ##
379 package CGI::WebOut::TieMediator;
380 #sub TIESCALAR { return $_[1] }
381 #sub TIEARRAY { return $_[1] }
382 #sub TIEHASH { return $_[1] }
383 16 ▶sub TIEHANDLE { return $_[1] }
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
384 1 }}}
385  
386  
387 1 {{{
388 ##
389 ## This class is used to tie objects to filehandle.
390 ## Synopsis:
391 ## tie(*STDOUT, "CGI::WebOut::Tie", \*STDOUT, tied(*STDOUT));
392 ## All the parent methods is virtually inherited. So you
393 ## may call print(*FH, ...), close(*FH, ...) etc.
394 ## All the output is redirected to current CGI::WebOut object.
395 ## This class is used internally by the main module.
396 ##
397 package CGI::WebOut::Tie;
398  
399 # The same as tie(), but ties existed object to the handle.
400 16 ▶sub tieobj {
Subroutine tieobj: called CC=1 SCAR=0.0
401 # return $_[1]? tie($_[0], "CGI::WebOut::TieMediator", $_[1]) : untie($_[0]);
402 16 return tie($_[0], "CGI::WebOut::TieMediator", $_[1]);
403 }
404  
405 ## Fully overriden methods.
406 0 ▶sub WRITE { shift; goto &CGI::WebOut::echo; }
Subroutine WRITE: not called CC=1 SCAR=6.9
407 19 ▶sub PRINT { shift; goto &CGI::WebOut::echo; }
Subroutine PRINT: called CC=1 SCAR=0.0
408 0 ▶sub PRINTF { shift; @_ = sprintf(@_); goto &CGI::WebOut::echo; }
Subroutine PRINTF: not called CC=1 SCAR=6.9
409  
410 # Creates the new tie. Saves the old object and handle reference.
411 # See synopsis above.
412 sub TIEHANDLE
413 2 ▶{ my ($cls, $handle, $prevObj) = @_;
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
414 2 CGI::WebOut::_Debug("TIEHANDLE(%s, %s, %s)", $cls, $handle, $prevObj);
415 2 return bless {
416 handle => $handle,
417 prevObj => $prevObj,
418 outObj => CGI::WebOut->_newRoot($rRootBuf),
419 }, $cls;
420 }
421  
422 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
423 2 CGI::WebOut::_Debug("[%s] DESTROY", $_[0]);
424 }
425  
426 ## Methods, inherited from parent.
427 sub CLOSE
428 0 ▶{ my ($this) = @_;
Subroutine CLOSE: not called CC=1 SCAR=6.9
429 0 CGI::WebOut::Flush();
430 0 ▶ $this->parentCall(sub { close(*{$this->{handle}}) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
431 }
432 sub BINMODE
433 0 ▶{ my ($this) = @_;
Subroutine BINMODE: not called CC=1 SCAR=6.9
434 0 ▶ $this->parentCall(sub { binmode(*{$this->{handle}}) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 }
436 sub FILENO
437 0 ▶{ my ($this) = @_;
Subroutine FILENO: not called CC=1 SCAR=6.9
438 # Do not call Flush() here, because it is incompatible with CGI::Session.
439 # E.g. the following code will not work if Flush() is uncommented:
440 # use CGI::WebOut;
441 # use CGI::Session;
442 # my $session = new CGI::Session(...);
443 # SetCookie(...); # says that "headers are already sent"
444 #CGI::WebOut::Flush();
445 0 ▶ $this->parentCall(sub { return fileno(*{$this->{handle}}) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
446 0 return 0;
447 }
448  
449 # Untie process is fully transparent for parent. For example, code:
450 # tie(*STDOUT, "T1");
451 # eval "use CGI::WebOut"; #***
452 # print "OK!";
453 # untie(*STDOUT);
454 # generates EXACTLY the same sequence of call to T1 class, as this
455 # code without ***-marked line.
456 # Unfortunately we cannot retie CGI::WebOut::Tie back to the object
457 # in UNTIE() - when the sub finishes, Perl hardly remove tie.
458 1 our $doNotUntie = 0;
459 sub UNTIE
460 14 ▶{ my ($this, $nRef) = @_;
Subroutine UNTIE: called CC=3 SCAR=11.0
461 14 ▶ return if $doNotUntie;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::WebOut::Tie::doNotUntie 13 1 14
462 1 my $handle = $this->{handle};
463 1 CGI::WebOut::_Debug("UNTIE prev=%s, cur=%s", $this->{prevObj}, tied(*$handle));
464 # Destroy output object BEFORE untie parent.
465 1 $this->{outObj} = undef;
466 # Untie parent object.
467 1 ▶ if ($this->{prevObj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"prevObj"}) 1 0 1
468 1 tieobj(*$handle, $this->{prevObj});
469 1 $this->{prevObj} = undef; # release ref
470 1 untie(*$handle); # call parent untie
471 1 $this->{prevObj} = tied(*$handle);
472 }
473 }
474  
475 # void method parentPrint(...)
476 # Prints using parent print method.
477 sub parentPrint
478 14 ▶{ my $this = shift;
Subroutine parentPrint: called CC=1 SCAR=0.0
479 14 my $params = \@_;
480 14 CGI::WebOut::_Debug("parentPrint('%s')", join "", @$params);
481 14 ▶ $this->parentCall(sub { print STDOUT @$params });
Subroutine __ANON__: called CC=1 SCAR=0.0
482 }
483  
484 # void method parentCall($codeRef)
485 # Calls $codeRef in the context of object, previously tied to handle.
486 # After call context is switched back, as if nothing has happened.
487 # Returns the same that $codeRef had returned.
488 sub parentCall
489 14 ▶{ my ($this, $sub) = @_;
Subroutine parentCall: called CC=7 SCAR=21.4
490 14 my ($handle, $obj) = ($this->{handle}, $this->{prevObj});
491 14 my $save = tied(*$handle);
492 14 ▶ if ($obj) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj) { } 1 13 14
elsif ($save) { } 13 0 13
493 1 tieobj(*$handle, $obj)
494 } elsif ($save) {
495 13 local $doNotUntie = 1;
496 13 local $^W;
497 13 untie(*$handle);
498 }
499 14 CGI::WebOut::_Debug("parentCall for STDOUT=%s", $obj);
500 14 ▶ my @result = eval { wantarray? $sub->() : scalar $sub->() };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
501 14 ▶ if ($save) {
Branch: 2 branches
BranchTrueFalseTotal
if ($save) { } 14 0 14
elsif ($obj) { } 0 0 0
502 14 tieobj(*$handle, $save);
503 } elsif ($obj) {
504 0 local $doNotUntie = 1;
505 0 local $^W;
506 0 untie(*$handle);
507 }
508 14 ▶ return wantarray? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 14 14
509 }
510 1 }}}
511  
512  
513 # Since v2.0 AutoLoader is not used.
514 #use AutoLoader 'AUTOLOAD';
515 #return 1;
516 #__END__
517  
518  
519 # Èñïîëüçîâàíèå try-catch-throw:
520 # try {
521 # êîä, êîòîðûé ìîæåò âûëåòåòü ïî îøèáêå
522 # èëè êîòîðûé ãåíåðèðóåò èñêëþ÷åíèå ñ ïîìîùüþ throw
523 # } catch {
524 # èìÿ èñêëþ÷åíèÿ èëè ñîîáùåíèå îá îøèáêå - â $_
525 # } warnings {
526 # ñïèñîê ïðîèçîøåäøèõ îøèáîê è ïðåäóïðåæäåíèé â @_
527 # }
528 # Áëîêè catch è warnings âûïîëíÿþòñÿ â ïîðÿäêå èõ ïîÿâëåíèÿ è ìîãóò îòñóòñòâîâàòü.
529 sub try (&;@)
530 1 ▶{ my ($try,@Hand) = @_;
Subroutine try: called CC=3 SCAR=11.1
Pod documented
531 # Ìû ÍÅ ìîæåì èñïîëüçîâàòü local äëÿ ñîõðàíåíèÿ @Errors ïî ñëåäóþùåé ïðèñèíå.
532 # Åñëè â &$try âûâåäóòñÿ ïðåäóïðåæäåíèÿ, à ïîòîì áóäåò âûçâàí exit(),
533 # òî äî êîíöà try() óïðàâëåíèå òàê è íå äîéäåò. Åñëè áû ìû èñïîëüçîâàëè
534 # local, òî ýòè ïðåäóïðåæäåíèÿ â @Errors ïîòåðÿëèñü áû. Òàê êàê èñïîëüçóåòñÿ
535 # ñîõðàíåíèå âî âðåìåííîé ïåðåìåííîé, ïðåäóïðåæäåíèÿ â @Errors îñòàíóòñÿ íà
536 # ìåñòå è âûâåäóòñÿ íà ýêðàí.
537 1 my @SvErrors = @Errors;
538 # Çàïóñêàåì try-áëîê
539 1 my @Result = eval { &$try };
540 # Óïðàâëåíèå ïîïàëî ñþäà, åñëè âíóòðè êîäà íå áûëî âûçîâà exit().
541 #  ïðîòèâíîì ñëó÷àå ïðîèñîäèò âûëåò èç ôóíêöèè è èç ïðîãðàììû.
542 # Ïîëó÷àåì âñå âîçíèêøèå ïðåäóïðåæäåíèÿ. Ïðè÷åì çàïèñûâàåì èõ â
543 # ïåðåìåííóþ òèïà local, ÷òîáû ýòà ïåðåìåííûÿ áûëà âèäíà âíóòðè
544 # warnings-ôóíêöèè (ñì. íèæå).
545 1 ▶ local @Warns = @Errors>@SvErrors? @Errors[@SvErrors..$#Errors] : ();
Branch: 1 branch
BranchTrueFalseTotal
@CGI::WebOut::Errors > @SvErrors ? : 1 0 1
546 # Âîññòàíàâëèâàåì ñîîáùåíèÿ îá îøèáêàõ
547 1 @Errors = @SvErrors;
548 # Çàïóñêàåì îáðàáîò÷èêè â ïîðÿäêå èõ ïîÿâëåíèÿ
549 1 map { &$_() } @Hand;
550 # Âîçâðàùàåì çíà÷åíèå, êîòîðîå âåðíóë try-áëîê
551 1 ▶ return wantarray? @Result: $Result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
552 }
553  
554 # Âîçâðàùàåò ôóíêöèþ-çàìûêàíèå, êîòîðàÿ âûçûâàåò òåëî catch-áëîêà.
555 sub catch(&;@)
556 1 ▶{ my ($body, @Hand)=@_;
Subroutine catch: called CC=1 SCAR=0.0
Pod documented
557 1 ▶ return (sub { if($@) { chomp($@); local $_=$@; &$body($_) } }, @Hand);
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
558 }
559  
560 # Âîçâðàùàåò ôóíêöèþ-çàìûêàíèå, êîòîðàÿ âûçûâàåò òåëî warnings-áëîêà.
561 sub warnings(&;@)
562 0 ▶{ my ($body,@Hand)=@_;
Subroutine warnings: not called CC=1 SCAR=6.9
Pod documented
563 0 ▶ return (sub { &$body(@Warns) }, @Hand);
Subroutine __ANON__: not called CC=1 SCAR=6.9
564 }
565  
566 # Âûáðàñûâàåò èñêëþ÷åíèå.
567 1 ▶sub throw($) {
Subroutine throw: called CC=2 SCAR=7.6
Pod documented
568 1 ▶ die(ref($_[0])? $_[0] : "$_[0]\n")
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
569 }
570  
571  
572 # bool SetAutoflush([bool $mode])
573 # Óñòàíàâëèâàåò ðåæèì ñáðîñà áóôåðà echo: åñëè $mode=1, òî ðàçðåøàåò åãî àâòîñáðîñ ïîñëå
574 # êàæäîãî âûâîäà print èëè echo, èíà÷å - çàïðåùàåò (ñáðîñ äîëæåí ïðîèçâîäèòüñÿ ïî Flush()).
575 # Âîçâðàùàåò ïðåäûäóùèé óñòàíîâëåííûé ðåæèì àâòîñáðîñà.
576 sub SetAutoflush(;$)
577 0 ▶{ my ($mode)=@_;
Subroutine SetAutoflush: not called CC=2 SCAR=17.9
Pod documented
578 0 my $old = $UseAutoflush;
579 0 ▶ if (defined $mode) { $UseAutoflush = $mode; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 0 0 0
580 0 return $old;
581 }
582  
583 # bool NoAutoflush()
584 # Çàïðåùàåò ñáðàñûâàòü áóôåð ïîñëå êàæäîãî echo.
585 # Âîçâðàùàåò ïðåäûäóùèé ñòàòóñ àâòîñáðîñà.
586 0 ▶sub NoAutoflush() {
Subroutine NoAutoflush: not called CC=1 SCAR=6.9
Pod documented
587 0 return SetAutoflush(0);
588 }
589  
590  
591 # bool UseAutoflush()
592 # Ðàçðàøàåò ñáðàñûâàòü áóôåð ïîñëå êàæäîãî echo.
593 # Âîçâðàùàåò ïðåäûäóùèé ñòàòóñ àâòîñáðîñà.
594 0 ▶sub UseAutoflush() {
Subroutine UseAutoflush: not called CC=1 SCAR=6.9
Pod documented
595 0 return SetAutoflush(1);
596 }
597  
598  
599 # Ïåðåíàïðàâëÿåò íà äðóãîé URL (ìîæåò áûòü âíóòðåííèì ðåäèðåêòîì)
600 sub Redirect($)
601 0 ▶{ my ($url) = @_;
Subroutine Redirect: not called CC=1 SCAR=6.9
Pod documented
602 0 $Redirected = Header("Location: $url");
603 0 exit;
604 }
605  
606  
607 # Ïåðåíàïðàâëÿåò ÁÐÀÓÇÅÐ íà äðóãîé URL
608 sub ExternRedirect($)
609 0 ▶{ my ($url) = @_;
Subroutine ExternRedirect: not called CC=3 SCAR=24.8
Pod documented
610 0 ▶ if ($url !~ /^\w+:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /^\w+:/) 0 0 0
611 # Îòíîñèòåëüíûé àäðåñ.
612 0 ▶ if ($url !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[^/]) 0 0 0
613 0 my $sn = $ENV{SCRIPT_NAME};
614 0 $sn =~ s{/+[^/]*$}{}sg;
615 0 $url = "$sn/$url";
616 }
617 # Äîáàâèòü èìÿ õîñòà.
618 0 $url = "http://$ENV{SERVER_NAME}$url";
619 }
620 0 $Redirected = Header("Location: $url");
621 0 exit;
622 }
623  
624  
625 # Çàïðåùàåò êýøèðîâàíèå äîêóìåíòà áðàóçåðîì
626 sub NoCache()
627 0 ▶{ return 1 if $NoCached++;
Subroutine NoCache: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::WebOut::NoCached++ 0 0 0
628 0 ▶ Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT") or return undef 0 0 0
629 0 ▶ Header("Last-Modified: ".gmtime(time)." GMT") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&Header('Last-Modified: ' . gmtime(time) . ' GMT') or return undef 0 0 0
630 0 ▶ Header("Cache-Control: no-cache, must-revalidate") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&Header("Cache-Control: no-cache, must-revalidate") or return undef 0 0 0
631 0 ▶ Header("Pragma: no-cache") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&Header("Pragma: no-cache") or return undef 0 0 0
632 0 return 1;
633 }
634  
635  
636 # int ErrorReporting([int $level])
637 # Óñòàíàâëèâàåò ðåæèì âûâîäà îøèáîê:
638 # 0 - îøèáêè íå âûâîäÿòñÿ
639 # 1 - îøèáêè âûâîäÿòñÿ â áðàóçåð
640 # 2 - îøèáêè âûâîäÿòñÿ â áðàóçåð â âèäå êîììåíòàðèåâ
641 # Åñëè ïàðàìåòð íå çàäàí, ðåæèì íå ìåíÿåòñÿ.
642 # Âîçâðàùàåò ïðåäûäóùèé ñòàòóñ âûâîäà.
643 sub ErrorReporting(;$)
644 0 ▶{ my ($lev)=@_;
Subroutine ErrorReporting: not called CC=2 SCAR=17.9
Pod documented
645 0 my $old = $ErrorReporting;
646 0 ▶ $ErrorReporting = $lev if defined $lev;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lev 0 0 0
647 0 return $old;
648 }
649  
650  
651 # Äîáàâëÿåò ñîîáùåíèå îá îøèáêå ê ìàññèâó îøèáîê.
652 sub Warning($)
653 1 ▶{ my ($msg)=@_;
Subroutine Warning: called CC=1 SCAR=0.0
Pod undocumented
654 1 push(@Errors, $msg);
655 }
656  
657  
658 # Ïå÷àòàåò âñå íàêîïèâøèåñÿ ñîîáùåíèÿ îá îøèáêàõ.
659 # Ýòà ôóíêöèÿ âûçûâàåòñÿ â ìîìåíò, êîãäà STDOUT íàõîäèòñÿ â "ïîäâåøåííîì" ñîñòîÿíèè,
660 # ïîýòîìó èñïîëüçîâàíèå print ÇÀÏÐÅÙÅÍÎ!!!
661 sub __PrintAllErrors()
662 0 ▶{ local $^W = undef;
Subroutine __PrintAllErrors: not called CC=13 SCAR=52.0
663 # http://forum.dklab.ru/perl/symbiosis/Fastcgi+WeboutUtechkaPamyati.html
664 0 ▶ if(!@Errors || !$ErrorReporting){
Branch: 1 branch
BranchTrueFalseTotal
unless (@CGI::WebOut::Errors and $CGI::WebOut::ErrorReporting) 0 0 0
Condition0%0/3
@CGI::WebOut::Errors and $CGI::WebOut::ErrorReporting
!ll&&!rl&&r
000
MC/DC0%0/2
@CGI::WebOut::Errors and $CGI::WebOut::ErrorReporting
@CGI::WebOut::Errors $CGI::WebOut::ErrorReporting
Truth table0%0/3
A and B
A= @CGI::WebOut::ErrorsB= $CGI::WebOut::ErrorReporting
ABresult
0X0
100
111
665 0 @Errors=();
666 0 return ;
667 }
668 0 ▶ if (IsWebMode) {
Branch: 1 branch
BranchTrueFalseTotal
if (&IsWebMode()) { } 0 0 0
669 0 ▶ if ($ErrorReporting == ER_Err2Browser) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::WebOut::ErrorReporting == ER_Err2Browser()) 0 0 0
670 # ìàëî ëè, êàêèå òàì áûëè òàáëèöû...
671 0 echo "</script>","</xmp>","</pre>","</table>"x6,"</tt>"x2,"</i>"x2,"</b>"x2,"</div>"x10,"\n";
672 }
673 0 my %wasErr=();
674 0 ▶ for (my $i=0; $i<@Errors; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @CGI::WebOut::Errors) 0 0 0
675 0 chomp(my $st = $Errors[$i]);
676 # Èñêëþ÷àåì äóáëèðóþùèåñÿ ñîîáùåíèÿ î ÔÀÒÀËÜÍÛÕ îøèáêàõ.
677 0 ▶ next if $wasErr{$st};
Branch: 1 branch
BranchTrueFalseTotal
if $wasErr{$st} 0 0 0
678 0 ▶ $wasErr{$st}=1 if $st =~ /^Fatal:/;
Branch: 1 branch
BranchTrueFalseTotal
if $st =~ /^Fatal:/ 0 0 0
679 # Âûâîäèì ñîîáùåíèå.
680 0 ▶ if ($ErrorReporting == ER_Err2Browser) {
Branch: 3 branches
BranchTrueFalseTotal
if ($CGI::WebOut::ErrorReporting == ER_Err2Browser()) { } 0 0 0
elsif ($CGI::WebOut::ErrorReporting == ER_Err2Comment()) { } 0 0 0
elsif ($CGI::WebOut::ErrorReporting == ER_Err2Plain()) { } 0 0 0
681 0 $st=~s/>/&gt;/sg;
682 0 $st=~s/</&lt;/sg;
683 0 $st=~s|^([a-zA-Z]+:)|<b>$1</b>|mgx;
684 0 $st=~s|\n|<br>\n&nbsp;&nbsp;&nbsp;&nbsp;|g;
685 0 my $s=$i+1;
686 0 ▶ for(my $i=length($s); $i<length(scalar(@Errors)); $i++) { $s="&nbsp;$s" }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length scalar @CGI::WebOut::Errors) 0 0 0
687 0 echo "<b><tt>$s)</tt></b> $st<br>\n";
688 } elsif ($ErrorReporting == ER_Err2Comment) {
689 0 echo "\n<!-- $st -->";
690 } elsif ($ErrorReporting == ER_Err2Plain) {
691 0 echo "\n$st";
692 }
693 }
694 } else {
695 0 foreach my $st (@Errors) { chomp($st); echo "\n$st" }
696 }
697 0 @Errors=();
698 }
699  
700 1 return 1;
701 __END__
↑ Summary