File Coverage

blib/lib/App/Greple/debug.pm
Criterion Covered Total %
statement 17 59 28.8
branch 0 16 0.0
condition n/a
subroutine 6 25 24.0
pod 0 19 0.0
total 23 119 19.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             debug - Greple module for debug control
4              
5             =head1 SYNOPSIS
6              
7             greple -dmc
8              
9             greple -Mdebug
10              
11             greple -Mdebug::on(getoptex)
12              
13             greple -Mdebug::on=getoptex
14              
15             =head1 DESCRIPTION
16              
17             Enable debug mode for specified target. Currently, following targets
18             are available.
19              
20             getoptex Getopt::EX
21             getopt Getopt::Long
22             alert -da Alert information
23             color -dc Color information
24             directory -dd Change directory information
25             file -df Show search file names
26             number -dn Show number of processing files
27             match -dm Match pattern
28             option -do Show command option processing
29             process -dp Exec ps command before exit
30             stat -ds Show statistic information
31             grep -dg Show grep internal state
32             filter -dF Show filter informaiton
33             unused -du Show unused 1-char option name
34              
35             When used without function call, default target is enabled; currently
36             C and C
37              
38             $ greple -Mdebug
39              
40             Specify required target with C function like:
41              
42             $ greple -Mdebug::on(color,match,option)
43              
44             $ greple -Mdebug::on=color,match,option
45              
46             Calling C enables all targets, except C and
47             C.
48              
49             Target name marked with C<-dx> can be enabled in that form. Following
50             commands are all equivalent.
51              
52             $ greple -Mdebug::on=color,match,option
53              
54             $ greple -dc -dm -do
55              
56             $ greple -dcmo
57              
58             =head1 EXAMPLE
59              
60             Next command will show how the module option is processed in
61             L module.
62              
63             $ greple -Mdebug::on=getoptex,option -Mdig something --dig .
64              
65             =cut
66              
67              
68             package App::Greple::debug;
69              
70 1     1   1038 use v5.24;
  1         3  
71 1     1   4 use warnings;
  1         1  
  1         34  
72 1     1   3 use Carp;
  1         1  
  1         69  
73 1     1   4 use Data::Dumper;
  1         2  
  1         53  
74              
75 1     1   3 use App::Greple::Common qw(%debug &FILELABEL);
  1         2  
  1         107  
76 1     1   571 use Getopt::Long;
  1         9607  
  1         4  
77              
78             my %flags = (
79             getoptex => \$Getopt::EX::Loader::debug,
80             getopt => { on => sub { main::configure_getopt('debug') },
81             off => sub { main::configure_getopt('no_debug') } },
82             alert => \$debug{a},
83             color => \$debug{c},
84             directory => \$debug{d},
85             file => \$debug{f},
86             number => \$debug{n},
87             match => \$debug{m},
88             misc => \$debug{m},
89             option => \$debug{o},
90             process => \$debug{p},
91             stat => \$debug{s},
92             grep => \$debug{g},
93             filter => \$debug{F},
94             unused => \$debug{u},
95             );
96              
97 0     0 0   sub getoptex { on ( getoptex => 1 ) }
98 0     0 0   sub getopt { on ( getopt => 1 ) }
99 0     0 0   sub alert { on ( alert => 1 ) }
100 0     0 0   sub color { on ( color => 1 ) }
101 0     0 0   sub directory { on ( directory => 1 ) }
102 0     0 0   sub file { on ( file => 1 ) }
103 0     0 0   sub number { on ( number => 1 ) }
104 0     0 0   sub match { on ( match => 1 ) }
105 0     0 0   sub misc { on ( match => 1 ) }
106 0     0 0   sub option { on ( option => 1 ) }
107 0     0 0   sub process { on ( process => 1 ) }
108 0     0 0   sub stat { on ( stat => 1 ) }
109 0     0 0   sub grep { on ( grep => 1 ) }
110 0     0 0   sub filter { on ( filter => 1 ) }
111 0     0 0   sub unused { on ( unused => 1 ) }
112              
113             my %exclude = map { $_ => 1 } qw(unused number);
114              
115             my @all_flags = grep { not $exclude{$_} } sort keys %flags;
116              
117             sub switch {
118 0     0 0   my %arg = @_;
119 0           my @keys = keys %arg;
120 0           while (@keys) {
121 0           my $key = shift @keys;
122 0           my $val = $arg{$key};
123 0 0         my $e = $flags{$key} or next;
124 0 0         if (ref $e eq 'ARRAY') {
125 0           unshift @keys, @{$e};
  0            
126 0           next;
127             }
128 0 0         if (ref $e eq 'HASH') {
129 0 0         if (my $sub = $e->{ $val ? 'on' : 'off' }) {
    0          
130 0 0         ref $sub eq 'CODE' or die;
131 0           $sub->();
132             }
133             } else {
134 0           ${$e} = $val;
  0            
135             }
136             }
137             }
138              
139             my @default = qw(getoptex option);
140              
141             sub switch_default {
142 0     0 0   switch map { $_ => $_[0] } @default;
  0            
143             }
144              
145             sub on {
146 0     0 0   my %arg = @_;
147 0           delete $arg{&FILELABEL}; # no entry when called from -M otption.
148              
149             # Clear default setting on first call.
150 0           state $called;
151 0 0         $called++ or switch_default 0;
152              
153 0 0         if (delete $arg{'all'}) {
154 0           map { $arg{$_} = 1 } @all_flags;
  0            
155             }
156              
157 0           switch(%arg);
158             }
159              
160             sub initialize {
161 0     0 0   switch_default 1;
162             }
163              
164             1;
165              
166             # LocalWords: getoptex getopt grep greple misc