Data-Crumbr
view release on metacpan or search on metacpan
#!/usr/bin/env perl
# ABSTRACT: Render data structures for easy searching and parsing
# PODNAME: crumbr
use strict;
use warnings;
use Carp;
use Pod::Usage qw< pod2usage >;
use Getopt::Long qw< :config gnu_getopt >;
use English qw< -no_match_vars >;
our $VERSION = '0.0.1';
use Data::Crumbr;
my %config = (
encoding => 'utf8',
output => '-',
profile => 'Default',
type => 'yaml',
);
GetOptions(
\%config,
qw<
usage! help! man! version!
encoding|e=s
output|o=s
profile|p=s
type|t=s
>
) or pod2usage(-verbose => 99, -sections => 'USAGE');
pod2usage(message => "$0 $VERSION", -verbose => 99, -sections => ' ')
if $config{version};
pod2usage(-verbose => 99, -sections => 'USAGE') if $config{usage};
pod2usage(-verbose => 99, -sections => 'USAGE|EXAMPLES|OPTIONS')
if $config{help};
pod2usage(-verbose => 2) if $config{man};
# Script implementation here
crumbr(
profile => $config{profile},
encoder => {output => get_output(\%config)}
)->(read_data(\%config, $ARGV[0]));
sub read_data {
my ($config, $filename) = @_;
$filename //= '-';
my $reader = __PACKAGE__->can("read_data_$config->{type}")
or die "unsupported input type $config->{type}\n";
return $reader->($config, $filename);
} ## end sub read_data
sub read_data_yaml {
my ($config, $filename) = @_;
my $unicode_string = slurp($filename, $config{encoding});
for my $class (qw< YAML::XS YAML YAML::Tiny >) {
eval "use $class";
next if $EVAL_ERROR;
my $function = $class->can('Load');
return $function->($unicode_string);
} ## end for my $class (qw< YAML::XS YAML YAML::Tiny >)
die "cannot find any YAML module\n";
return;
} ## end sub read_data_yaml
sub read_data_json {
my ($config, $filename) = @_;
my $unicode_string = slurp($filename, $config{encoding});
require Encode;
my $utf8_octets =
Encode::encode('UTF-8', $unicode_string, Encode::FB_CROAK());
my @cs = qw< Cpanel::JSON::XS JSON::XS Mojo::JSON JSON::PP >;
for my $class (@cs) {
eval "use $class";
next if $EVAL_ERROR;
my $function = $class->can('decode_json');
my $retval = eval { $function->($utf8_octets) };
die "errors parsing input: $EVAL_ERROR\n"
if $EVAL_ERROR;
return $retval;
} ## end for my $class (@cs)
die "cannot find any JSON module\n";
return;
} ## end sub read_data_json
sub slurp_octets {
require Encode;
return Encode::encode('UTF-8', slurp(@_), Encode::FB_CROAK());
}
sub slurp {
my ($filename, $encoding) = @_;
my $fh = \*STDIN;
if ($filename ne '-') {
$fh = undef;
open $fh, '<', $filename
or die "open('$filename'): $OS_ERROR\n";
}
binmode $fh, ":encoding($encoding)";
local $INPUT_RECORD_SEPARATOR = undef; # slurp mode
return <$fh>;
} ## end sub slurp
sub get_output {
my ($config) = @_;
my $fh = \*STDOUT;
if ($config->{output} ne '-') {
$fh = undef;
open $fh, '>', $config->{output}
or die "open('$config->{output}'): $OS_ERROR\n";
}
binmode $fh, ":encoding($config->{encoding})";
return $fh;
} ## end sub get_output
__END__
=pod
=encoding UTF-8
=head1 NAME
crumbr - Render data structures for easy searching and parsing
=head1 VERSION
version 0.1.2
=head1 DESCRIPTION
( run in 2.990 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )