File Coverage

blib/lib/IO/Extended.pm
Criterion Covered Total %
statement 56 101 55.4
branch 19 44 43.1
condition 0 2 0.0
subroutine 17 29 58.6
pod 3 8 37.5
total 95 184 51.6


line stmt bran cond sub pod time code
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             require 5.005_62;
63            
64 3     3   32596 use strict;
  3         7  
  3         105  
65            
66 3     3   15 use warnings;
  3         5  
  3         79  
67            
68 3     3   16 use Carp;
  3         8  
  3         264  
69            
70 3     3   15 use Exporter;
  3         4  
  3         437  
71            
72             our @ISA = qw(Exporter);
73            
74             our %EXPORT_TAGS = ( 'all' => [ qw( printl println ln lne printfln lnf sprintfl sprintfln warnfln diefln ind indn indb indstr indblock tabs nl ) ] );
75            
76             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
77            
78             our @EXPORT = qw(println printfln);
79            
80             our $VERSION = '1.0';
81            
82             # Preloaded methods go here.
83            
84 3     3   3432 use subs qw( printl println ln lne lnf printfln sprintfln warnfln diefln ind indn indb indstr tabs indblock );
  3         127  
  3         15  
85            
86             our $_indentation = 0;
87            
88             our @_indenthistory = ( 0 );
89            
90             our $_newline = "\n";
91            
92             =head1 VARIABLES
93            
94             =over
95            
96             =item $IO::Extended::space
97            
98             =cut
99            
100             our $space = ' ';
101            
102             =item $IO::Extended::tabsize
103            
104             Scalars for constructing tabs. Indentation is done via printing C.
105            
106             =cut
107            
108             our $tabsize = 5;
109            
110             our $percent_s_quote = "'";
111            
112             sub _translate_fmt
113             {
114            
115 336     336   928 $_[0] =~ s/((?
  3         21  
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             {
128 113 100   113   160 print indstr() if indstr();
129            
130 113         621 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             {
141            
142 112 100   112   241 push @_, $_ unless @_;
143            
144 112         181 return printl @_, $_newline;
145             }
146            
147             =item ln
148            
149             Synonymou to println.
150            
151             =cut
152            
153             sub ln
154             {
155 0     0   0 println(@_);
156             }
157            
158             =item printfln
159            
160             =cut
161            
162             sub printfln
163             {
164 222     222   297 my $fmt = shift;
165            
166 222         318 _translate_fmt( $fmt );
167            
168 222         268 $fmt .= $_newline;
169            
170 222 100       358 if( indstr() )
171             {
172 181         254 $fmt = indstr().$fmt;
173             }
174            
175 222         458 for( @_ )
176             {
177 332 50       762 carp "undefined value interpolation" unless defined $_ ;
178             }
179            
180 222         1550 return printf $fmt, @_;
181             }
182            
183             =item lnf
184            
185             Synonymou to printfln.
186            
187             =cut
188            
189             sub lnf
190             {
191 0     0   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             {
202 0     0 1 0 my $fmt = shift;
203            
204 0         0 _translate_fmt( $fmt );
205            
206 0 0       0 if( indstr() )
207             {
208 0         0 $fmt = indstr().$fmt;
209             }
210            
211 0         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             {
222 114     114   144 my $fmt = shift;
223            
224 114         194 _translate_fmt( $fmt );
225            
226 114         138 $fmt .= $_newline;
227            
228 114 100       162 if( indstr() )
229             {
230 90         141 $fmt = indstr().$fmt;
231             }
232            
233 114         956 return sprintf $fmt, @_;
234             }
235            
236             =item warnfln
237            
238             As C, but accepts a FORMAT string like printfln.
239            
240             =cut
241            
242             sub warnfln
243             {
244 3     3   9 warn( sprintfln( @_ ) );
245             }
246            
247             =item diefln
248            
249             As C, but accepts a FORMAT string like printfln.
250            
251             =cut
252            
253             sub diefln
254             {
255 1     1   3 die( sprintfln( @_ ) );
256             }
257            
258             =item ind( $integer )
259            
260             Sets the indentation value.
261            
262             =cut
263            
264             sub ind
265             {
266 111     111   304 my $indval = shift;
267            
268 111 100       218 if( defined $indval )
269             {
270 67 50       107 if( $indval >= 0 )
271             {
272 67 100       138 if( $_indenthistory[-1] != $indval )
273             {
274 56         91 push( @_indenthistory, $_indentation = $indval );
275             }
276             }
277             else
278             {
279 0         0 die 'indentation value is out of rang (>=0)';
280             }
281             }
282            
283 111         250 return $_indentation+0;
284             }
285            
286             =item indn
287            
288             Increases the indentation one value up.
289            
290             =cut
291            
292             sub indn
293             {
294 0   0 0   0 my $indval = ind() || 0;
295            
296 0         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             {
307 55 50   55   133 if( @_indenthistory > 0)
308             {
309 55         64 pop @_indenthistory;
310            
311 55 50       129 $_indentation = $_indenthistory[-1] if @_indenthistory;
312             }
313             else
314             {
315 0 0       0 $_indentation-- if $_indentation > 0;
316             }
317            
318 55         179 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             {
329 0     0 1 0 @_indenthistory = ( 0 );
330            
331 0         0 $_indentation = 0;
332            
333 0         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             {
344 11     11   37 my $size = shift;
345            
346 11 50       29 if( $size >= 0 )
347             {
348 11         15 $tabsize = $size;
349             }
350            
351 11         22 return $tabsize;
352             }
353            
354             =item indstr
355            
356             Returns the absolute indentation space.
357            
358             =cut
359            
360             sub indstr
361             {
362 932 100   932   1998 return '' unless $_indentation;
363            
364 859         2649 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             {
381 0     0 1   return join (' ', @_ ).$_newline;
382             }
383            
384             sub indblock
385             {
386 0     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 3     3   4524 use Carp;
  3         6  
  3         1374  
404            
405             sub printfln
406             {
407 0 0   0 0   @_ >= 2 or die 'usage: $io->printf(FMT,[ARGS])';
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 0         if( IO::Extended::indstr() )
418             {
419 0           $fmt = IO::Extended::indstr().$fmt;
420             }
421            
422 0           for( @_ )
423             {
424 0 0         carp "undefined value interpolation" unless defined $_ ;
425             }
426            
427 0           return printf $this $fmt, @_;
428             }
429            
430             sub lnf
431             {
432 0     0 0   printfln(@_);
433             }
434            
435             sub println
436             {
437 0 0   0 0   @_ or die 'usage: $io->print(ARGS)';
438            
439 0           my $this = shift;
440            
441 0 0         if( my $indent = IO::Extended::indstr() )
442             {
443 0           print $this $indent;
444             }
445            
446 0 0         push @_, $_ unless @_;
447            
448 0           print $this @_ , $_newline;
449             }
450            
451             sub ln
452             {
453 0     0 0   println(@_);
454             }
455            
456             sub printl
457             {
458 0 0   0 0   @_ or die 'usage: $io->print(ARGS)';
459            
460 0           my $this = shift;
461            
462 0 0         if( my $indent = IO::Extended::indstr() )
463             {
464 0           print $this $indent;
465             }
466            
467 0           print $this @_;
468             }
469            
470            
471             1;
472             __END__