App-DPath
view release on metacpan or search on metacpan
lib/App/DPath.pm view on Meta::CPAN
if (!defined reftype $entry) { # SCALAR
$output .= "$key:"._format_flat_inner_scalar($entry)."\n";
}
elsif (reftype $entry eq 'ARRAY') {
$output .= "$key:"._format_flat_inner_array($opt, $entry)."\n";
}
elsif (reftype $entry eq 'HASH') {
$output .= "$key:"._format_flat_inner_hash($opt, $entry)."\n";
}
else {
die "dpath: can not flatten data structure (".reftype($entry).").\n";
}
}
}
else {
die "dpath: can not flatten data structure (".reftype($result).") - try other output format.\n";
}
return $output;
}
sub _format_flat
{
my ($opt, $resultlist) = @_;
my $output = "";
$opt->{separator} = ";" unless defined $opt->{separator};
$output .= _format_flat_outer($opt, $_) foreach @$resultlist;
return $output;
}
sub write_out
{
my ($opt, $resultlist) = @_;
my $output = "";
my $outtype = $opt->{outtype} || 'yaml';
if ($outtype eq "yaml")
{
require YAML::Any;
if ($opt->{'yaml-module'}) {
@YAML::Any::_TEST_ORDER=($opt->{'yaml-module'});
} else {
@YAML::Any::_TEST_ORDER=(qw(YAML::XS YAML::Old YAML YAML::Tiny)); # no YAML::Syck
}
$output .= YAML::Any::Dump($resultlist);
}
elsif ($outtype eq "json")
{
eval "use JSON -convert_blessed_universally";
my $json = JSON->new->allow_nonref->pretty->allow_blessed->convert_blessed;
$output .= $json->encode($resultlist);
}
elsif ($outtype eq "ini") {
require Config::INI::Serializer;
my $ini = Config::INI::Serializer->new;
$output .= $ini->serialize($resultlist);
}
elsif ($outtype eq "dumper")
{
require Data::Dumper;
$output .= Data::Dumper::Dumper($resultlist);
}
elsif ($outtype eq "xml")
{
require XML::Simple;
my $xs = new XML::Simple;
$output .= $xs->XMLout($resultlist, AttrIndent => 1, KeepRoot => 1);
}
elsif ($outtype eq "flat") {
$output .= _format_flat( $opt, $resultlist );
}
else
{
die "dpath: unrecognized output format: $outtype.";
}
return $output;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::DPath - Cmdline tool around Data::DPath
=head1 SYNOPSIS
use App::DPath;
my $path = '//some/dpath';
my $data = App::DPath::read_in ($file);
my @resultlist = dpath($path)->match($data);
my $output = App::DPath::write_out ({}, \@resultlist);
print $output;
=head1 DESCRIPTION
This module handles the input and output for the L<dpath> command.
=head1 SUBROUTINES
=head2 read_in
my $data = App::DPath::read_in ($file, $intype, $yamlmod);
read_in takes a filename as its mandatory argument. It reads the data
from the file according to the type specified in the second argument
(which defaults to 'yaml') and returns the resulting data structure. Other
data types are: 'json', 'xml', 'ini', 'cfggeneral', 'dumper' and 'tap'.
The optional third argument specifies a list of modules to use to parse
YAML. The first available module in the list is used. If unspecified it
defaults to L<YAML::XS>, L<YAML::Old>, L<YAML> and L<YAML::Tiny>.
( run in 0.688 second using v1.01-cache-2.11-cpan-39bf76dae61 )