File Coverage

bin/dpath
Criterion Covered Total %
statement 44 48 91.6
branch 6 10 60.0
condition 2 7 28.5
subroutine 13 14 92.8
pod n/a
total 65 79 82.2


line stmt bran cond sub pod time code
1             #! /usr/bin/perl
2             # PODNAME: dpath
3             # ABSTRACT: cmdline tool around Data::DPath
4              
5 30     30   169495 use 5.008;
  30         110  
6 30     30   161 use strict;
  30         46  
  30         1190  
7 30     30   200 use warnings;
  30         109  
  30         1552  
8              
9 30     30   27728 use App::Rad;
  30         443010  
  30         224  
10 30     30   20350 use App::DPath;
  30         188  
  30         1401  
11 30     30   25572 use Data::DPath 'dpath';
  30         3337423  
  30         255  
12              
13             ######################################################################
14             #
15             # App::Rad interface
16             #
17             ######################################################################
18              
19             BEGIN {
20 30     30   10547 my $default_cmd = "search";
21 30 50 33     9584 unshift @ARGV, $default_cmd unless $ARGV[0] && $ARGV[0] =~ /^(search|help)$/;
22             }
23              
24 30         11034531 App::Rad->run();
25              
26             sub setup
27             {
28 30     30   70612 my $c = shift;
29 30         165 $c->unregister_command("help");
30 30         538 $c->register_commands("help", "search");
31             }
32              
33             sub help
34             {
35 0     0   0 my ($c) = @_;
36              
37 0         0 return "dpath [-ios] [--fb] [ --fi]
38              
39             -o
40             --outtype - output format
41             [yaml(default), json, dumper, xml]
42             -i
43             --intype - input format
44             [yaml(default), json, dumper, xml, tap, taparchive, ini]
45             --yaml-module - which YAML module to use for format 'yaml'
46             [YAML::XS, YAML, YAML::Old, YAML::Tiny, YAML::Syck]
47             (by default it uses what YAML::Any finds, but not YAML::Syck)
48             -s
49             --separator - sub entry separator for output format 'flat'
50             (default=;)
51             --fb - on output format 'flat' use [brackets] around
52             outer arrays
53             --fi - on output format 'flat' prefix outer array lines
54             with index
55              
56             See 'perldoc Data::DPath' for how to specify a DPath.
57             ";
58             }
59              
60             sub search :Help(search incoming data by DPath (default commmand))
61             {
62 60     60   64206 my ($c) = @_;
63              
64 60         222 _getopt($c);
65              
66 30         319 my $path = $c->argv->[0];
67 30   50     205 my $file = $c->argv->[1] || '-';
68             my $data = App::DPath::read_in( $file, $c->options->{intype},
69 30         293 $c->options->{'yaml-module'} );
70              
71 26         142 my $out;
72 26 100       198 foreach my $datum (ref $data eq ref [] ? @$data : $data) {
73 26         143 my $result = _match($c, $datum, $path);
74 26         467 $out .= App::DPath::write_out($c->options, $result);
75             }
76 24         2659 return $out;
77 30     30   290 }
  30         52  
  30         285  
78              
79 30     30   75198 sub default { search(@_) }
80              
81             ######################################################################
82             #
83             # Implementation
84             #
85             ######################################################################
86             sub _match
87             {
88 26     26   101 my ($c, $data, $path) = @_;
89              
90 26 50       170 if (not $data) {
91 0         0 die "dpath: no input data to match.\n";
92             }
93              
94 26         201 my @resultlist = dpath($path)->match($data);
95 26         194847 return \@resultlist;
96             }
97              
98             sub _getopt
99             {
100 60     60   133 my ($c) = @_;
101              
102 60 50 0     1250 $c->getopt( "faces|f=i",
103             "times|t=i",
104             "intype|i=s",
105             "outtype|o=s",
106             "separator|s=s",
107             "yaml-module",
108             "fb",
109             "fi",
110             )
111             or help() and return undef;
112 30 50       549597 if (not $c->argv->[0]) {
113 0           die "dpath: please specify a dpath.\n";
114             }
115             }
116              
117             __END__