blib/lib/IO/Extended.pm

filter: Statementstmt 60.8% 70 / 115 Branchbran 43.1% 19 / 44 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 58.6% 17 / 29 Podpod 37.5% 3 / 8 totaltotal 54.7% 109 / 199 CC 27 SCAR 19.8
CC
27
Cov
55%
printfln
30.0
println
20.0
printl
12.0
CRAP 7.2
↑ Summary
1 # Author: Murat Uenalan (muenalan@cpan.org)
2 #
3 # Copyright (c) 2001 Murat Uenalan. All rights reserved.
4 #
5 # Note: This program is free software; you can redistribute
6 #
7 # it and/or modify it under the same terms as Perl itself.
8
9 =pod
10
11 =head1 NAME
12
13 IO::Extended - more print functions
14
15 =head1 SYNOPSIS
16
17 use IO::Extended ':all';
18
19 printl 'foo bar';
20
21 println 'foo bar';
22
23 ln "foo bar";
24
25 printfln 'foo %s', 'bar';
26
27 $str = sprintfln 'foo %s', 'bar';
28
29 warnfln 'foo %s', 'bar';
30
31 diefln 'foo %s', 'bar';
32
33 tabs 5;
34
35 ind 1;
36
37 indn;
38
39 print indblock( "alpha\nbeta\ngamma\n" );
40
41 indb;
42
43 indstr;
44
45 nl( 'string' );
46
47 =head1 DESCRIPTION
48
49 IO::Extended contains a bunch of print-like functions, which automatically add
50 newline characters to the string.
51
52 =head1 EXPORT
53
54 qw(println printfln) by default.
55
56 Use the ':all' for automatically adding the complete set.
57
58 =cut
59
60 package IO::Extended;
61
62 4 require 5.005_62;
63
64 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67
68 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69
70 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
71
72 4 our @ISA = qw(Exporter);
73
74 4 our %EXPORT_TAGS = ( 'all' => [ qw( printl println ln lne printfln lnf sprintfl sprintfln warnfln diefln ind indn indb indstr indblock tabs nl ) ] );
75
76 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
77
78 4 our @EXPORT = qw(println printfln);
79
80 4 our $VERSION = '1.0';
81
82 # Preloaded methods go here.
83
84 4 ▶use subs qw( printl println ln lne lnf printfln sprintfln warnfln diefln ind indn indb indstr tabs indblock );
Subroutine BEGIN: called CC=1 SCAR=0.0
85
86 4 our $_indentation = 0;
87
88 4 our @_indenthistory = ( 0 );
89
90 4 our $_newline = "\n";
91
92 =head1 VARIABLES
93
94 =over
95
96 =item $IO::Extended::space
97
98 =cut
99
100 4 our $space = ' ';
101
102 =item $IO::Extended::tabsize
103
104 Scalars for constructing tabs. Indentation is done via printing C<space x ( indentation * tabsize )>.
105
106 =cut
107
108 4 our $tabsize = 5;
109
110 4 our $percent_s_quote = "'";
111
112 sub _translate_fmt
113 669 ▶{
Subroutine _translate_fmt: called CC=1 SCAR=0.0
114
115 669 $_[0] =~ s/((?<!%)%[\d\-\.]*[S|D])/"$percent_s_quote".lc $1."$percent_s_quote"/ge;
116 }
117
118 =head1 FUNCTIONS
119
120 =item printl
121
122 Same as normal print, but with indentation.
123
124 =cut
125
126 sub printl
127 224 ▶{
Subroutine printl: called CC=2 SCAR=6.9
128 224 ▶ print indstr() if indstr();
Branch: 1 branch
BranchTrueFalseTotal
if indstr() 181 43 224
129
130 224 return print @_;
131 }
132
133 =item println
134
135 Same as normal print, but adds newline character to the end.
136
137 =cut
138
139 sub println
140 223 ▶{
Subroutine println: called CC=2 SCAR=6.9
141
142 223 ▶ push @_, $_ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 221 223
143
144 223 return printl @_, $_newline;
145 }
146
147 =item ln
148
149 Synonymou to println.
150
151 =cut
152
153 sub ln
154 0 ▶{
Subroutine ln: not called CC=1 SCAR=6.9
155 0 println(@_);
156 }
157
158 =item printfln
159
160 =cut
161
162 sub printfln
163 442 ▶{
Subroutine printfln: called CC=4 SCAR=13.9
164 442 my $fmt = shift;
165
166 442 _translate_fmt( $fmt );
167
168 442 $fmt .= $_newline;
169
170 442 ▶ if( indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (indstr()) 361 81 442
171 {
172 361 $fmt = indstr().$fmt;
173 }
174
175 442 for( @_ )
176 {
177 662 ▶ carp "undefined value interpolation" unless defined $_ ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 662 662
178 }
179
180 442 return printf $fmt, @_;
181 }
182
183 =item lnf
184
185 Synonymou to printfln.
186
187 =cut
188
189 sub lnf
190 0 ▶{
Subroutine lnf: not called CC=1 SCAR=6.9
191 0 printfln(@_);
192 }
193
194 =item sprintfl
195
196 Same as normal (s)printf, but has some extensions to the FORMAT string.
197
198 =cut
199
200 sub sprintfl
201 0 ▶{
Subroutine sprintfl: not called CC=2 SCAR=17.9
Pod documented
202 0 my $fmt = shift;
203
204 0 _translate_fmt( $fmt );
205
206 0 ▶ if( indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (indstr()) 0 0 0
207 {
208 0 $fmt = indstr().$fmt;
209 }
210
211 0 return sprintf $fmt, @_;
212 }
213
214 =item sprintfln
215
216 Same as normal (s)printf, but adds newline character to the FORMAT string (Result).
217
218 =cut
219
220 sub sprintfln
221 227 ▶{
Subroutine sprintfln: called CC=2 SCAR=6.9
222 227 my $fmt = shift;
223
224 227 _translate_fmt( $fmt );
225
226 227 $fmt .= $_newline;
227
228 227 ▶ if( indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (indstr()) 180 47 227
229 {
230 180 $fmt = indstr().$fmt;
231 }
232
233 227 return sprintf $fmt, @_;
234 }
235
236 =item warnfln
237
238 As C<warn>, but accepts a FORMAT string like printfln.
239
240 =cut
241
242 sub warnfln
243 5 ▶{
Subroutine warnfln: called CC=1 SCAR=0.0
244 5 warn( sprintfln( @_ ) );
245 }
246
247 =item diefln
248
249 As C<die>, but accepts a FORMAT string like printfln.
250
251 =cut
252
253 sub diefln
254 2 ▶{
Subroutine diefln: called CC=1 SCAR=0.0
255 2 die( sprintfln( @_ ) );
256 }
257
258 =item ind( $integer )
259
260 Sets the indentation value.
261
262 =cut
263
264 sub ind
265 221 ▶{
Subroutine ind: called CC=4 SCAR=14.0
266 221 my $indval = shift;
267
268 221 ▶ if( defined $indval )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indval) 133 88 221
269 {
270 133 ▶ if( $indval >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($indval >= 0) { } 133 0 133
271 {
272 133 ▶ if( $_indenthistory[-1] != $indval )
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Extended::_indenthistory[-1] != $indval) 111 22 133
273 {
274 111 push( @_indenthistory, $_indentation = $indval );
275 }
276 }
277 else
278 {
279 0 die 'indentation value is out of rang (>=0)';
280 }
281 }
282
283 221 return $_indentation+0;
284 }
285
286 =item indn
287
288 Increases the indentation one value up.
289
290 =cut
291
292 sub indn
293 0 ▶{
Subroutine indn: not called CC=2 SCAR=17.9
294 0 ▶ my $indval = ind() || 0;
Condition0%0/2
ind() || 0
l!l
00
MC/DC0%0/1
ind() || 0
ind()
Truth table0%0/2
A
A= ind()
Aresult
11
00
295
296 0 return ind( $indval + 1 )+0;
297 }
298
299 =item indb
300
301 Decreases the indentation on back in its history.
302
303 =cut
304
305 sub indb
306 110 ▶{
Subroutine indb: called CC=4 SCAR=17.1
307 110 ▶ if( @_indenthistory > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@IO::Extended::_indenthistory > 0) { } 110 0 110
308 {
309 110 pop @_indenthistory;
310
311 110 ▶ $_indentation = $_indenthistory[-1] if @_indenthistory;
Branch: 1 branch
BranchTrueFalseTotal
if @IO::Extended::_indenthistory 110 0 110
312 }
313 else
314 {
315 0 ▶ $_indentation-- if $_indentation > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Extended::_indentation > 0 0 0 0
316 }
317
318 110 return $_indentation+0;
319 }
320
321 =item indreset
322
323 Sets indentation to zero and resets its history.
324
325 =cut
326
327 sub indreset
328 0 ▶{
Subroutine indreset: not called CC=1 SCAR=6.9
Pod documented
329 0 @_indenthistory = ( 0 );
330
331 0 $_indentation = 0;
332
333 0 return $_indentation+0;
334 }
335
336 =item tabs( $integer )
337
338 Sets the tabsize for indentation. Returns the actual tabsize if parameter is omitted.
339
340 =cut
341
342 sub tabs
343 22 ▶{
Subroutine tabs: called CC=2 SCAR=7.0
344 22 my $size = shift;
345
346 22 ▶ if( $size >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= 0) 22 0 22
347 {
348 22 $tabsize = $size;
349 }
350
351 22 return $tabsize;
352 }
353
354 =item indstr
355
356 Returns the absolute indentation space.
357
358 =cut
359
360 sub indstr
361 1857 ▶{
Subroutine indstr: called CC=2 SCAR=6.9
362 1857 ▶ return '' unless $_indentation;
Branch: 1 branch
BranchTrueFalseTotal
unless $IO::Extended::_indentation 143 1714 1857
363
364 1714 return $space x ( $_indentation * $tabsize );
365 }
366
367 =item nl
368
369 join ' ', @_ and adds a newline to it. No indentation.
370
371 print nl( "my cool", "text" )
372
373 results in
374
375 "my cool text\n"
376
377 =cut
378
379 sub nl
380 0 ▶{
Subroutine nl: not called CC=1 SCAR=6.9
Pod documented
381 0 return join (' ', @_ ).$_newline;
382 }
383
384 sub indblock
385 0 ▶{
Subroutine indblock: not called CC=2 SCAR=17.9
386 0 @_ = split /$_newline/, $_[0];
387
388 0 my @result;
389
390 0 my $i = indstr();
391
392 0 for( @_ )
393 {
394 0 s/^\s*/$i/gmi;
395 0 push @result, $_;
396 }
397
398 0 return (@result, $_newline);
399 }
400
401 package IO::Handle;
402
403 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
404
405 sub printfln
406 0 ▶{
Subroutine printfln: not called CC=5 SCAR=34.0
Pod undocumented
407 0 ▶ @_ >= 2 or die 'usage: $io->printf(FMT,[ARGS])';
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or die "usage: \$io->printf(FMT,[ARGS])" 0 0 0
408
409 0 my $this = shift;
410
411 0 my $fmt = shift;
412
413 0 IO::Extended::_translate_fmt( $fmt );
414
415 0 $fmt .= $_newline;
416
417 0 ▶ if( IO::Extended::indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (IO::Extended::indstr()) 0 0 0
418 {
419 0 $fmt = IO::Extended::indstr().$fmt;
420 }
421
422 0 for( @_ )
423 {
424 0 ▶ carp "undefined value interpolation" unless defined $_ ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
425 }
426
427 0 return printf $this $fmt, @_;
428 }
429
430 sub lnf
431 0 ▶{
Subroutine lnf: not called CC=1 SCAR=6.9
Pod undocumented
432 0 printfln(@_);
433 }
434
435 sub println
436 0 ▶{
Subroutine println: not called CC=4 SCAR=30.0
Pod undocumented
437 0 ▶ @_ or die 'usage: $io->print(ARGS)';
Branch: 1 branch
BranchTrueFalseTotal
@_ or die "usage: \$io->print(ARGS)" 0 0 0
438
439 0 my $this = shift;
440
441 0 ▶ if( my $indent = IO::Extended::indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $indent = IO::Extended::indstr()) 0 0 0
442 {
443 0 print $this $indent;
444 }
445
446 0 ▶ push @_, $_ unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
447
448 0 print $this @_ , $_newline;
449 }
450
451 sub ln
452 0 ▶{
Subroutine ln: not called CC=1 SCAR=6.9
Pod undocumented
453 0 println(@_);
454 }
455
456 sub printl
457 0 ▶{
Subroutine printl: not called CC=3 SCAR=24.8
Pod undocumented
458 0 ▶ @_ or die 'usage: $io->print(ARGS)';
Branch: 1 branch
BranchTrueFalseTotal
@_ or die "usage: \$io->print(ARGS)" 0 0 0
459
460 0 my $this = shift;
461
462 0 ▶ if( my $indent = IO::Extended::indstr() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $indent = IO::Extended::indstr()) 0 0 0
463 {
464 0 print $this $indent;
465 }
466
467 0 print $this @_;
468 }
469
470
471 4 1;
472 __END__
↑ Summary