blib/script/stag-handle.pl untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 154 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 # POD docs at end
4  
5 use strict;
6  
7 use Data::Stag qw(:all);
8 use Getopt::Long;
9  
10 use FileHandle;
11  
12 my $exec;
13 my $codefile;
14 my $parser = '';
15 my $errhandler = "";
16 my $errf;
17 my $writer = '';
18 my %trap = ();
19 my $datafmt;
20 my $module;
21 my @units;
22 GetOptions("codefile|c=s"=>\$codefile,
23 "sub|s=s"=>\$exec,
24 "trap|t=s%"=>\%trap,
25 "parser|format|p=s" => \$parser,
26 "errhandler=s" => \$errhandler,
27 "errf|e=s" => \$errf,
28 "writer|w=s"=>\$writer,
29 "data|d=s"=>\$datafmt,
30 "module|m=s"=>\$module,
31 "units|u=s@"=>\@units,
32 "help|h"=>sub {system("perldoc $0");exit 0},
33 );
34  
35 if (!$codefile && !$exec && !$module) {
36 $codefile = shift @ARGV if (@ARGV > 1);
37 die "you must supply -c or -m or -s, or provide codefile"
38 unless $codefile;
39 }
40 my @files = @ARGV;
41  
42 $errhandler = Data::Stag->getformathandler($errhandler || 'xml');
43 if ($errf) {
44 $errhandler->file($errf);
45 }
46 else {
47 $errhandler->fh(\*STDERR);
48 }
49 my $catch = {};
50 no strict;
51 if ($exec) {
52 $catch = eval $exec;
53 if ($@) {
54 die $@;
55 }
56 if (ref($catch) ne 'HASH') {
57 print STDERR "exec \"$exec\" is not a hash";
58 exit 1;
59 }
60 }
61 if ($codefile) {
62 $catch = do "$codefile";
63 if ($@) {
64 print STDERR "\n\nstag-handle error:\n";
65 print STDERR "There was an error with the codefile \"$codefile\":\n\n";
66 die $@;
67 }
68 if (ref($catch) ne 'HASH') {
69 print STDERR "codefile \"$codefile\" does not return a hash";
70 exit 1;
71 }
72 }
73 if (%trap) {
74 #
75 # die Dumper \%trap;
76 %$catch = (%$catch, %trap);
77 }
78 use strict;
79 my @events;
80 my $inner_handler;
81 if ($module) {
82 $inner_handler = Data::Stag->makehandler($module);
83 } else {
84 my $meth = $exec ? $exec : $codefile;
85 if (!%$catch) {
86 die "method \"$meth\" did not return handler";
87 }
88 if (!ref($catch) || ref($catch) ne "HASH") {
89 die("$meth must return hashref");
90 }
91 $inner_handler = Data::Stag->makehandler(%$catch);
92 @events = keys %$catch;
93 }
94 if (@units) {
95 @events = @units;
96 }
97 $inner_handler->errhandler($errhandler);
98 my $h = Data::Stag->chainhandlers([@events],
99 $inner_handler,
100 $writer);
101  
102 while (my $fn = shift @files) {
103 my $fh;
104 if (!$fn || $fn eq '-') {
105 $fh = \*STDIN;
106 $fn = '';
107 }
108 else {
109 $fh = FileHandle->new($fn) || die "Cannot open file: $fn";
110 }
111
112 my $p = Data::Stag->parser(-file=>$fn, -format=>$parser,
113 -errhandler=>$errhandler);
114  
115 $p->handler($h);
116 $p->parse_fh($fh);
117 if ($datafmt) {
118 print $inner_handler->stag->$datafmt();
119 }
120 }
121  
122 __END__