| 1 | package Catmandu::Fix::format; | ||
| 2 | |||
| 3 | 1 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Catmandu::Fix::Has; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | has path => (fix_arg => 1); | |
| 12 | 1 | has spec => (fix_arg => 1); | |
| 13 | |||
| 14 | 1 | with 'Catmandu::Fix::SimpleGetValue'; | |
| 15 | |||
| 16 | sub emit_value { | ||
| 17 | 3 | ▶ | my ($self, $var, $fixer) = @_; |
|
Subroutine
emit_value: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 18 | 3 | my $spec = $fixer->emit_string($self->spec); | |
| 19 | |||
| 20 | 3 | "if (is_array_ref(${var})) {" | |
| 21 | . "${var} = sprintf(${spec},\@{${var}});" | ||
| 22 | . "} elsif (is_hash_ref(${var})) {" | ||
| 23 | . "${var} = sprintf(${spec},\%{${var}});" | ||
| 24 | . "} elsif (is_string(${var})) {" | ||
| 25 | . "${var} = sprintf(${spec},${var});" . "}"; | ||
| 26 | } | ||
| 27 | |||
| 28 | 1 | 1; | |
| 29 | |||
| 30 | __END__ | ||
| 31 | |||
| 32 | =pod | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Catmandu::Fix::format - replace the value with a formatted (sprintf-like) version | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | # e.g. number: 41 | ||
| 41 | format(number,"%-10.10d") # number => "0000000041" | ||
| 42 | |||
| 43 | # e.g. numbers: | ||
| 44 | # - 41 | ||
| 45 | # - 15 | ||
| 46 | format(number,"%-10.10d %-5.5d") # numbers => "0000000041 00015" | ||
| 47 | |||
| 48 | # e.g. hash: | ||
| 49 | # name: Albert | ||
| 50 | format(name,"%-10s: %s") # hash: "name : Albert" | ||
| 51 | |||
| 52 | # e.g. array: | ||
| 53 | # - 1 | ||
| 54 | format(array,"%d %d %d") # Fails! The array contains only one value, but you request 3 values | ||
| 55 | |||
| 56 | # Test first if the array contains 3 values | ||
| 57 | if exists(array.2) | ||
| 58 | format(array,"%d %d %d") | ||
| 59 | end | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | Create a string formatted by the usual printf conventions of the C library function sprintf. | ||
| 64 | See L<http://perldoc.perl.org/functions/sprintf.html> for a complete description. | ||
| 65 | |||
| 66 | =head1 SEE ALSO | ||
| 67 | |||
| 68 | L<Catmandu::Fix> , L<sprintf> | ||
| 69 | |||
| 70 | =cut | ||