| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package YATT::Lite::XHF::Dumper; | 
| 2 | 2 |  |  | 2 |  | 1120 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 75 |  | 
| 3 | 2 |  |  | 2 |  | 10 | use warnings qw(FATAL all NONFATAL misc); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 134 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = "0.02"; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 10 | use Exporter qw(import); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 128 |  | 
| 7 |  |  |  |  |  |  | our @EXPORT_OK = qw(dump_xhf); | 
| 8 |  |  |  |  |  |  | our @EXPORT = @EXPORT_OK; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 92 | use 5.010; | 
|  | 2 |  |  |  |  | 13 |  | 
| 11 | 2 |  |  | 2 |  | 12 | use Carp; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 174 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 2 |  |  | 2 |  | 10 | use YATT::Lite::XHF qw($cc_name); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 1713 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub dump_xhf { | 
| 16 | 17 |  |  | 17 | 0 | 12104 | shift; | 
| 17 | 17 |  |  |  |  | 41 | _dump_pairs(@_); | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub _dump_pairs { | 
| 21 | 23 |  |  | 23 |  | 28 | my @buffer; | 
| 22 | 23 |  |  |  |  | 59 | while (@_) { | 
| 23 | 38 | 100 | 100 |  |  | 348 | if (@_ == 1 or not defined $_[0] or ref $_[0]) { | 
|  |  | 100 | 100 |  |  |  |  | 
| 24 | 12 |  |  |  |  | 24 | push @buffer, _dump_value(shift, '-'); | 
| 25 |  |  |  |  |  |  | } elsif ($_[0] =~ m{^$cc_name+$}) { | 
| 26 | 22 |  |  |  |  | 48 | push @buffer, shift() . _dump_value(shift, ':'); | 
| 27 |  |  |  |  |  |  | } else { | 
| 28 |  |  |  |  |  |  | # ('', undef) => "-\n= #null" | 
| 29 | 4 |  |  |  |  | 11 | push @buffer, '-' . escape(shift), _dump_value(shift, '-'); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 | 23 |  |  |  |  | 162 | join "\n", @buffer; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub _dump_value { | 
| 36 |  |  |  |  |  |  | # value part. | 
| 37 | 52 | 100 |  | 52 |  | 162 | unless (defined $_[0]) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 38 | 9 |  |  |  |  | 31 | "= #null"; | 
| 39 |  |  |  |  |  |  | } elsif (not ref $_[0]) { | 
| 40 | 32 |  |  |  |  | 59 | $_[1] . escape(shift); | 
| 41 |  |  |  |  |  |  | } elsif (ref $_[0] eq 'ARRAY') { | 
| 42 | 9 |  |  |  |  | 21 | dump_array(shift); | 
| 43 |  |  |  |  |  |  | } elsif (ref $_[0] eq 'HASH') { | 
| 44 | 2 |  |  |  |  | 6 | dump_hash(shift); | 
| 45 |  |  |  |  |  |  | } else { | 
| 46 | 0 |  |  |  |  | 0 | croak "Can't dump ref(@{[ref $_[0]]}) as XHF: $_[0]"; | 
|  | 0 |  |  |  |  | 0 |  | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub escape { | 
| 51 | 36 |  |  | 36 | 0 | 59 | my ($str) = @_; | 
| 52 | 36 |  |  |  |  | 39 | my $sep = do { | 
| 53 | 36 | 100 | 66 |  |  | 204 | if ($str =~ s/\n$// or $str =~ /^\s+|\s+$/s) { | 
| 54 | 3 |  |  |  |  | 7 | "\n " | 
| 55 |  |  |  |  |  |  | } else { | 
| 56 | 33 |  |  |  |  | 61 | " " | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | }; | 
| 59 | 36 |  |  |  |  | 52 | $str =~ s/\n/\n /g; | 
| 60 | 36 |  |  |  |  | 184 | $sep . $str; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub dump_array { | 
| 64 | 9 |  |  | 9 | 0 | 11 | my ($item) = @_; | 
| 65 | 9 |  |  |  |  | 16 | "[\n" . join("\n", do { | 
| 66 | 9 | 100 | 66 |  |  | 87 | if (@$item and @$item % 2 == 0 and looks_like_hash($item)) { | 
|  |  |  | 100 |  |  |  |  | 
| 67 | 4 |  |  |  |  | 10 | _dump_pairs(@$item); | 
| 68 |  |  |  |  |  |  | } else { | 
| 69 | 5 |  |  |  |  | 10 | map {_dump_value($_, '-')} @$item | 
|  | 14 |  |  |  |  | 30 |  | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | }) . "\n]"; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub looks_like_hash { | 
| 75 | 5 |  |  | 5 | 0 | 7 | my ($item) = @_; | 
| 76 | 5 |  |  |  |  | 15 | for (my $i = 0; $i < @$item; $i += 2) { | 
| 77 | 9 | 100 | 66 |  |  | 114 | return 0 if ref($item->[$i]) or $item->[$i] !~ m{^$cc_name+$}; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 4 |  |  |  |  | 17 | return 1; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub dump_hash { | 
| 83 | 2 |  |  | 2 | 0 | 3 | my ($item) = @_; | 
| 84 | 2 |  |  |  |  | 12 | "{\n" . _dump_pairs(map {$_, $item->{$_}} sort keys %$item) . "\n}"; | 
|  | 5 |  |  |  |  | 16 |  | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | __END__ |