File: stag-query.pl

package info (click to toggle)
libdata-stag-perl 0.14-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,480 kB
  • sloc: perl: 6,394; lisp: 141; xml: 116; ansic: 55; makefile: 17; sh: 2
file content (115 lines) | stat: -rwxr-xr-x 1,740 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/local/bin/perl -w

# POD docs at end


use strict;

use Data::Stag qw(:all);
use Getopt::Long;


use strict;

use Carp;
use Data::Stag qw(:all);
use Getopt::Long;

my $parser = "";
my $handler = "";
my $mapf;
my $tosql;
my $toxml;
my $toperl;
my $debug;
my $help;
my @add = ();
my $lc;
my $merge;
GetOptions(
           "help|h"=>\$help,
           "parser|format|p=s" => \$parser,
           "handler|writer|w=s" => \$handler,
           "xml"=>\$toxml,
           "perl"=>\$toperl,
           "lc"=>\$lc,
           "debug"=>\$debug,
	   "merge=s"=>\$merge,
          );
if ($help) {
    system("perldoc $0");
    exit 0;
}


my $funcname = shift @ARGV;
my $match = shift @ARGV;
my @files = @ARGV;

my $func;
my $agg;
if ($funcname =~ /^sub /) {
    $func = eval $funcname;
    if ($@) {
	die $@;
    }
}
elsif ($funcname eq 'sum') {
    $func = 
      sub {
	  $agg += $_ foreach @_;
      };
}
elsif ($funcname eq 'avg') {
    $func = 
      sub {
	  $agg += $_ foreach @_;
	  $agg = $agg/@_;
      };
}
elsif ($funcname eq 'cat') {
    $func = 
      sub {
	  $agg .= $_ foreach @_;
      };
}
else {
    die $funcname;
}

my @matches = ();
my $H = Data::Stag->makehandler($match => sub {
				    my $self = shift;
				    my $stag = shift;
				    my $data = $stag->data;
				    push(@matches, $data);
				});
foreach my $fn (@files) {
    Data::Stag->parse(-file=>$fn, -handler=>$H);
    $func->(@matches);
    print "$agg\n";
}
exit 0;

__END__

=head1 NAME 

stag-query - aggregare queries

=head1 SYNOPSIS

  stag-query avg person/age file.xml

  stag-query sum person/salary file.xml

  stag-query 'sub { $agg .= ", ".shift }' person/name file.xml

=head1 DESCRIPTION

Performs aggregate queries

=head1 ARGUMENTS

=cut