File Coverage

blib/lib/Data/Unixish/sprintf.pm
Criterion Covered Total %
statement 56 58 96.5
branch 10 16 62.5
condition 6 6 100.0
subroutine 14 14 100.0
pod 1 1 100.0
total 87 95 91.5


line stmt bran cond sub pod time code
1             package Data::Unixish::sprintf;
2              
3 1     1   469 use 5.010;
  1         5  
4 1     1   383 use locale;
  1         515  
  1         6  
5 1     1   32 use strict;
  1         1  
  1         18  
6 1     1   360 use syntax 'each_on_array'; # to support perl < 5.12
  1         19892  
  1         3  
7 1     1   2925 use warnings;
  1         2  
  1         22  
8             #use Log::Any '$log';
9              
10 1     1   358 use Data::Unixish::Util qw(%common_args);
  1         2  
  1         105  
11 1     1   5 use POSIX qw(locale_h);
  1         2  
  1         6  
12 1     1   506 use Scalar::Util 'looks_like_number';
  1         2  
  1         376  
13              
14             our $VERSION = '1.572'; # VERSION
15              
16             our %SPEC;
17              
18             $SPEC{sprintf} = {
19             v => 1.1,
20             summary => 'Apply sprintf() on input',
21             description => <<'_',
22              
23             Array will also be processed (all the elements are fed to sprintf(), the result
24             is a single string), unless `skip_array` is set to true.
25              
26             Non-numbers can be skipped if you use `skip_non_number`.
27              
28             Undef, hashes, and other non-scalars are ignored.
29              
30             _
31             args => {
32             %common_args,
33             format => {
34             schema=>['str*'],
35             cmdline_aliases => { f=>{} },
36             req => 1,
37             pos => 0,
38             },
39             skip_non_number => {
40             schema=>[bool => default=>0],
41             },
42             skip_array => {
43             schema=>[bool => default=>0],
44             },
45             },
46             tags => [qw/formatting itemfunc text/],
47             };
48             sub sprintf {
49 2     2 1 5 my %args = @_;
50 2         4 my ($in, $out) = ($args{in}, $args{out});
51 2         3 my $format = $args{format};
52              
53 2         3 my $orig_locale = _sprintf_begin();
54              
55 2         14 while (my ($index, $item) = each @$in) {
56 8         17 push @$out, _sprintf_item($item, \%args);
57             }
58              
59 2         8 _sprintf_end(\%args, $orig_locale);
60              
61 2         9 [200, "OK"];
62             }
63              
64             sub _sprintf_begin {
65 4     4   22 my $orig_locale = setlocale(LC_ALL);
66 4 50       12 if ($ENV{LC_NUMERIC}) {
    0          
    0          
67 4         15 setlocale(LC_NUMERIC, $ENV{LC_NUMERIC});
68             } elsif ($ENV{LC_ALL}) {
69 0         0 setlocale(LC_ALL, $ENV{LC_ALL});
70             } elsif ($ENV{LANG}) {
71 0         0 setlocale(LC_ALL, $ENV{LANG});
72             }
73 4         10 return $orig_locale;
74             }
75              
76             sub _sprintf_end {
77 4     4   7 my ($args, $orig_locale) = @_;
78 4         47 setlocale(LC_ALL, $orig_locale);
79             }
80              
81             sub _sprintf_item {
82 16     16   23 my ($item, $args) = @_;
83              
84             {
85 16 100       16 last unless defined($item);
  16         28  
86              
87 12         26 my $r = ref($item);
88 12 100 100     36 if ($r eq 'ARRAY' && !$args->{skip_array}) {
89 1     1   6 no warnings;
  1         2  
  1         83  
90 2         7 $item = CORE::sprintf($args->{format}, @$item);
91 2         3 last;
92             }
93 10 100       15 last if $r;
94 8 50       14 last if $item eq '';
95 8 100 100     30 last if !looks_like_number($item) && $args->{skip_non_number};
96             {
97 1     1   5 no warnings;
  1         7  
  1         75  
  6         7  
98 6         38 $item = CORE::sprintf($args->{format}, $item);
99             }
100             }
101 16         74 return $item;
102             }
103              
104             1;
105             # ABSTRACT: Apply sprintf() on input
106              
107             __END__