| 1 | 19 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 19 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package FusqlFS::Formatter; | ||||||||||||
| 5 | 19 | ▶ | use FusqlFS::Version; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 19 | our $VERSION = $FusqlFS::Version::VERSION; | |||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | FusqlFS::Formatter - formatter backend for FusqlFS | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | my ($dump, $load) = FusqlFS::Formatter->init('yaml'); | ||||||||||||
| 15 | print $dump->($data); | ||||||||||||
| 16 | my $data = $load->($data); | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||
| 19 | |||||||||||||
| 20 | This class is a frontend to different data formatters for FusqlFS. | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 METHODS | ||||||||||||
| 23 | |||||||||||||
| 24 | =over | ||||||||||||
| 25 | |||||||||||||
| 26 | =cut | ||||||||||||
| 27 | |||||||||||||
| 28 | 19 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | our %FORMATTERS = ( | ||||||||||||
| 31 | xml => [ | ||||||||||||
| 32 | 'XML/Simple.pm', | ||||||||||||
| 33 | 0 | ▶ | sub { XMLout($_[0], NoAttr => 1) }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 34 | 0 | ▶ | sub { XMLin($_[0], NoAttr => 1) }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 35 | 19 | ], | |||||||||||
| 36 | yaml => [ | ||||||||||||
| 37 | 'YAML/Tiny.pm', | ||||||||||||
| 38 | \&YAML::Tiny::Dump, | ||||||||||||
| 39 | \&YAML::Tiny::Load, | ||||||||||||
| 40 | ], | ||||||||||||
| 41 | json => [ | ||||||||||||
| 42 | 'JSON/Syck.pm', | ||||||||||||
| 43 | \&JSON::Syck::Dump, | ||||||||||||
| 44 | \&JSON::Syck::Load, | ||||||||||||
| 45 | ], | ||||||||||||
| 46 | ); | ||||||||||||
| 47 | |||||||||||||
| 48 | =item init | ||||||||||||
| 49 | |||||||||||||
| 50 | Initialize dumper and loader for given output format. | ||||||||||||
| 51 | |||||||||||||
| 52 | Input: $format | ||||||||||||
| 53 | Output: &dumper, &loader | ||||||||||||
| 54 | |||||||||||||
| 55 | =begin testing init | ||||||||||||
| 56 | |||||||||||||
| 57 | #!noinst | ||||||||||||
| 58 | my ($dump, $load) = FusqlFS::Formatter->init('native'); | ||||||||||||
| 59 | is ref $dump, 'CODE', 'Dumper defined'; | ||||||||||||
| 60 | is ref $load, 'CODE', 'Loader defined'; | ||||||||||||
| 61 | |||||||||||||
| 62 | =end testing | ||||||||||||
| 63 | =cut | ||||||||||||
| 64 | sub init | ||||||||||||
| 65 | 3 | ▶ | { | ||||||||||
|
Subroutine
init: called CC=7 SCAR=26.3
Pod
documented
| |||||||||||||
| 66 | 3 | my $class = shift; | |||||||||||
| 67 | 3 | ▶ | my $format = shift||'yaml'; | ||||||||||
|
Condition50%1/2
shift() || 'yaml'
MC/DC0%0/1
shift() || 'yaml'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 68 | |||||||||||||
| 69 | 3 | my $package; | |||||||||||
| 70 | my $formatter; | ||||||||||||
| 71 | |||||||||||||
| 72 | 3 | ▶ | $formatter = $FORMATTERS{$format} || [ | ||||||||||
|
Condition50%1/2
$FusqlFS::Formatter::FORMATTERS{$format} || [package_file($package = 'FusqlFS::Formatter::' . ucfirst(lc $format)), eval "\\&${package}::Dump", eval "\\&${package}::Load"]
MC/DC0%0/1
$FusqlFS::Formatter::FORMATTERS{$format} || [package_file($package = 'FusqlFS::Formatter::' . ucfirst(lc $format)), eval "\\&${package}::Dump", eval "\\&${package}::Load"]
$FusqlFS::Formatter::FORMATTERS{$format}
Truth table50%1/2
A
A= $FusqlFS::Formatter::FORMATTERS{$format}
| |||||||||||||
| 73 | package_file($package = 'FusqlFS::Formatter::'.ucfirst(lc($format))), | ||||||||||||
| 74 | eval qq{\\&${package}::Dump}, | ||||||||||||
| 75 | eval qq{\\&${package}::Load}, | ||||||||||||
| 76 | ]; | ||||||||||||
| 77 | |||||||||||||
| 78 | 3 | ▶ | if ($formatter->[0]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 3 | eval { require $formatter->[0]; }; | |||||||||||
| 80 | 3 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | ▶ | croak "Failed to load native formatter" if ($format eq 'native'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | if ($format eq 'yaml') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | carp "Failed to load formatter `yaml', falling back to `native'"; | |||||||||||
| 85 | 0 | return $class->init('native'); | |||||||||||
| 86 | } else { | ||||||||||||
| 87 | 0 | carp "Failed to load formatter `$format', falling back to `yaml'"; | |||||||||||
| 88 | 0 | return $class->init('yaml'); | |||||||||||
| 89 | } | ||||||||||||
| 90 | } | ||||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 3 | return $formatter->[1], $formatter->[2]; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 3 | ▶ | sub package_file { | ||||||||||
|
Subroutine
package_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 97 | 3 | my $package = shift; | |||||||||||
| 98 | 3 | $package =~ s{::}{/}g; | |||||||||||
| 99 | 3 | $package .= '.pm'; | |||||||||||
| 100 | 3 | return $package; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | 19 | 1; | |||||||||||
| 104 | |||||||||||||
| 105 | __END__ | ||||||||||||