| 1 | package Data::Unixish::sprintfn; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use locale; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | #use Log::Any '$log'; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use POSIX qw(locale_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Text::sprintfn (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 16 | 2 | our $DATE = '2025-02-24'; # DATE | |||||||||||||||||||||||||||
| 17 | 2 | our $DIST = 'Data-Unixish'; # DIST | |||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '1.574'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | our %SPEC; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $SPEC{sprintfn} = { | ||||||||||||||||||||||||||||
| 23 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 24 | summary => 'Like sprintf, but use sprintfn() from Text::sprintfn', | ||||||||||||||||||||||||||||
| 25 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Unlike in *sprintf*, with this function, hash will also be processed. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | MARKDOWN | ||||||||||||||||||||||||||||
| 30 | args => { | ||||||||||||||||||||||||||||
| 31 | %common_args, | ||||||||||||||||||||||||||||
| 32 | format => { | ||||||||||||||||||||||||||||
| 33 | schema=>['str*'], | ||||||||||||||||||||||||||||
| 34 | cmdline_aliases => { f=>{} }, | ||||||||||||||||||||||||||||
| 35 | req => 1, | ||||||||||||||||||||||||||||
| 36 | pos => 0, | ||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | skip_non_number => { | ||||||||||||||||||||||||||||
| 39 | schema=>[bool => default=>0], | ||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | skip_array => { | ||||||||||||||||||||||||||||
| 42 | schema=>[bool => default=>0], | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | skip_hash => { | ||||||||||||||||||||||||||||
| 45 | schema=>[bool => default=>0], | ||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | tags => [qw/formatting itemfunc text/], | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub sprintfn { | ||||||||||||||||||||||||||
|
Subroutine
sprintfn: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 52 | 1 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | my $orig_locale = _sprintfn_begin(\%args); | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | while (my ($index, $item) = each @$in) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 4 | push @$out, _sprintfn_item($item, \%args); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 1 | _sprintfn_end(\%args, $orig_locale); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | [200, "OK"]; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub _sprintfn_begin { | ||||||||||||||||||||||||||
|
Subroutine
_sprintfn_begin: called CC=4 SCAR=17.2
| |||||||||||||||||||||||||||||
| 64 | 2 | my $args = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my $orig_locale = setlocale(LC_ALL); | |||||||||||||||||||||||||||
| 67 | 2 | ▶ | if ($ENV{LC_NUMERIC}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 68 | 0 | setlocale(LC_NUMERIC, $ENV{LC_NUMERIC}); | |||||||||||||||||||||||||||
| 69 | } elsif ($ENV{LC_ALL}) { | ||||||||||||||||||||||||||||
| 70 | 0 | setlocale(LC_ALL, $ENV{LC_ALL}); | |||||||||||||||||||||||||||
| 71 | } elsif ($ENV{LANG}) { | ||||||||||||||||||||||||||||
| 72 | 0 | setlocale(LC_ALL, $ENV{LANG}); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | return $orig_locale; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | sub _sprintfn_item { | ||||||||||||||||||||||||||
|
Subroutine
_sprintfn_item: called CC=10 SCAR=26.5
| |||||||||||||||||||||||||||||
| 78 | 8 | my ($item, $args) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 8 | ▶ | last unless defined($item); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 6 | my $r = ref($item); | |||||||||||||||||||||||||||
| 83 | 6 | ▶ | if ($r eq 'ARRAY' && !$args->{skip_array}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$r eq "ARRAY" and not $args->{"skip_array"}
MC/DC0%0/2
$r eq "ARRAY" and not $args->{"skip_array"}
$r eq "ARRAY"
not $args->{"skip_array"}
Truth table33%1/3
A and not(B)
A= $r eq "ARRAY"B= not $args->{"skip_array"}
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 0 | $item = Text::sprintfn::sprintfn($args->{format}, @$item); | |||||||||||||||||||||||||||
| 86 | 0 | last; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 6 | ▶ | if ($r eq 'HASH' && !$args->{skip_hash}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r eq "HASH" and not $args->{"skip_hash"}
MC/DC50%1/2
$r eq "HASH" and not $args->{"skip_hash"}
$r eq "HASH"
not $args->{"skip_hash"}
Truth table66%2/3
A and not(B)
A= $r eq "HASH"B= not $args->{"skip_hash"}
| |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 4 | $item = Text::sprintfn::sprintfn($args->{format}, $item); | |||||||||||||||||||||||||||
| 91 | 4 | last; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | last if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | last if $item eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | last if !looks_like_number($item) && $args->{skip_non_number}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not &looks_like_number($item) and $args->{"skip_non_number"}
MC/DC0%0/2
not &looks_like_number($item) and $args->{"skip_non_number"}
not &looks_like_number($item)
$args->{"skip_non_number"}
Truth table0%0/3
not(A) and B
A= not &looks_like_number($item)B= $args->{"skip_non_number"}
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 98 | 0 | $item = Text::sprintfn::sprintfn($args->{format}, $item); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 8 | return $item; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub _sprintfn_end { | ||||||||||||||||||||||||||
|
Subroutine
_sprintfn_end: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 2 | my ($args, $orig_locale) = @_; | |||||||||||||||||||||||||||
| 106 | 2 | setlocale(LC_ALL, $orig_locale); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | 1; | |||||||||||||||||||||||||||
| 110 | # ABSTRACT: Like sprintf, but use sprintfn() from Text::sprintfn | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||