File Coverage

lib/App/GitFind/Base.pm
Criterion Covered Total %
statement 25 50 50.0
branch 0 14 0.0
condition 1 4 25.0
subroutine 7 13 53.8
pod 5 5 100.0
total 38 86 44.1


line stmt bran cond sub pod time code
1             package App::GitFind::Base;
2              
3 5     5   195590 use 5.010;
  5         29  
4 5     5   26 use strict;
  5         7  
  5         91  
5 5     5   21 use warnings;
  5         6  
  5         218  
6              
7             our $VERSION = '0.000002';
8              
9 5     5   756 use parent 'Exporter'; # TODO use Exporter::Tidy instead?
  5         472  
  5         31  
10             use vars::i [
11 5         33 '$VERBOSE' => 0,
12             '$QUIET' => 0,
13             '@EXPORT' => [qw(true false
14             croak ddc getparameters *QUIET _qwc *VERBOSE vlog vwarn)],
15 5     5   1974 ];
  5         3203  
16              
17             #use Import::Into;
18              
19 5     5   570 use constant { true => !!1, false => !!0 };
  5         9  
  5         3030  
20              
21             # Re-exports
22             #use Carp qw(confess);
23             #use Data::Dumper::Compact ();
24             #use Getargs::Mixed;
25              
26             # === Documentation === {{{1
27              
28             =head1 NAME
29              
30             App::GitFind::Base - base definitions for App::GitFind
31              
32             =head1 SYNOPSIS
33              
34             use App::GitFind::Base;
35              
36             Imports the functions described herein. Does not set L<strict> and L<warnings>
37             in the caller, since invoking modules have to do so on their own anyway for the
38             sake of Kwalitee.
39              
40             =head1 VARIABLES
41              
42             =head2 $QUIET
43              
44             Set to a truthy value to disable logging via L</vlog>. Overrides L</$VERBOSE>.
45             Exported as C<*QUIET> so that it can be C<local>ized.
46              
47             =head2 $VERBOSE
48              
49             Set to a positive integer to enable logging via L</vlog>.
50             Exported as C<*VERBOSE> so that it can be C<local>ized.
51              
52             =head1 FUNCTIONS
53              
54             =cut
55              
56             # }}}1
57              
58             =head2 _qwc
59              
60             qw(), but permitting comments. Call as C<< _qwc(<<EOT) >>. Thanks to ideas at
61             https://www.perlmonks.org/?node=qw%20comments . Prototyped as C<($)>.
62              
63             Has a leading underscore because for some reason that makes my syntax files
64             happier!
65              
66             =cut
67              
68             sub _qwc ($) {
69 20     20   13329 my @retval;
70 20   50     213 for(split "\n", $_[0]//'') {
71 70         136 chomp;
72 70         265 s{#.*$}{}; # Remove comments
73 70         235 s{(?:^\s+)|(?:\s+$)}{}g; # Remove leading/trailing ws
74 70         160 push @retval, grep { length } split /\s+/;
  85         190  
75             }
76 20         335 return @retval;
77             } #_qwc()
78              
79             =head2 getparameters
80              
81             An alias of the C<parameters()> function from L<Getargs::Mixed>, but with
82             C<-undef_ok> set.
83              
84             =cut
85              
86             sub getparameters {
87 0     0 1   state $GM = (require Getargs::Mixed, Getargs::Mixed->new(-undef_ok => true));
88              
89 0           unshift @_, $GM;
90 0           goto &Getargs::Mixed::parameters;
91             } #getparameters()
92              
93             =head2 ddc
94              
95             L<Data::Dumper::Compact/ddc>, but lazily loads C<Data::Dumper::Compact>.
96              
97             =cut
98              
99             sub ddc {
100 0     0 1   state $dumpcb = (require Data::Dumper::Compact, Data::Dumper::Compact->new->dump_cb);
101 0           goto &$dumpcb;
102             }
103              
104             =head2 croak
105              
106             As L<Carp/croak>, but lazily loads C<Carp>.
107              
108             =cut
109              
110             sub croak {
111 0     0 1   require Carp;
112 0           goto &Carp::croak;
113             }
114              
115             =head2 vlog
116              
117             Log information to STDERR if L</$VERBOSE> is set. Usage:
118              
119             vlog { <list of things to log> }
120             [optional min verbosity level (default 1)]
121             [, log-routine args];
122              
123             The items in the list are joined by C<' '> on output, and a C<'\n'> is added.
124             Each line is prefixed with C<'# '> for the benefit of test runs.
125             To break the list across multiple lines, specify C<\n> at the beginning of
126             a list item.
127              
128             The list is in C<{}> so that it won't be evaluated if logging is turned off.
129             It is a full block, so you can run arbitrary code to decide what to log.
130             If the block returns an empty list, vlog will not produce any output.
131             However, if the block returns at least one element, vlog will produce at
132             least a C<'# '>.
133              
134             The message will be output only if L</$VERBOSE> is at least the given minimum
135             verbosity level (1 by default).
136              
137             If C<< $VERBOSE >= 4 >>, the filename and line from which vlog was called
138             will also be printed.
139              
140             If more arguments are provided than two, the extras are the arguments
141             to the subroutine. This permits you to pass arguments from the caller's
142             C<@_> that would otherwise be shadowed inside the logging routine. E.g.:
143              
144             sub foo {
145             vlog { $_[0] } 1, $_[1]; # log foo's $_[1]
146             }
147              
148             =cut
149              
150             sub vlog (&;@) {
151 0 0   0 1   return if $QUIET;
152 0           my ($crRoutine, $level) = splice @_, 0, 2;
153 0 0 0       return unless $VERBOSE >= ($level // 1);
154              
155 0           my @log = $crRoutine->(@_);
156 0 0         return unless @log;
157              
158 0 0         chomp $log[$#log] if $log[$#log];
159             # TODO add an option to number the lines of the output
160 0           my $msg = join(' ', @log);
161 0           $msg =~ s/^/# /gm;
162              
163 0 0         if($VERBOSE >= 4) {
164 0           my ($package, $filename, $line) = caller;
165 0           $msg .= " (at $filename:$line)";
166             }
167              
168 0           say STDERR $msg;
169             } #vlog()
170              
171             =head2 vwarn
172              
173             As L</vlog>, but warns regardless of L</$VERBOSE>. Does respect L</$QUIET>.
174              
175             =cut
176              
177             sub vwarn (&) {
178 0 0   0 1   return if $QUIET;
179              
180 0           my @log = &{$_[0]}();
  0            
181 0 0         return unless @log;
182              
183 0     0     vlog { "Warning:", @log } $VERBOSE;
  0            
184             } #vwarn()
185              
186             =head2 import
187              
188             See L</SYNOPSIS>
189              
190             =cut
191              
192             #sub import {
193             # my $target = caller;
194             # $_[0]->export_to_level(1, @_); # Symbols
195             #
196             # #$_->import::into($target) foreach qw(strict warnings); # Pragmas
197             # # ... each module has to import those anyway to satisfy Kwalitee.
198             #
199             # #Carp->import::into($target, qw(carp croak confess cluck)); # Packages
200             # #Data::Dumper::Compact->import::into($target, 'ddc');
201             # #Getargs::Mixed->import::into($target);
202             #} #import()
203              
204             1;
205             __END__
206             # === Rest of the docs === {{{1
207              
208             =head1 AUTHOR
209              
210             Christopher White, C<< <cxw at cpan.org> >>
211              
212             =head1 LICENSE AND COPYRIGHT
213              
214             Copyright 2019 Christopher White.
215             Portions copyright 2019 D3 Engineering, LLC.
216              
217             This program is distributed under the MIT (X11) License:
218             L<http://www.opensource.org/licenses/mit-license.php>
219              
220             Permission is hereby granted, free of charge, to any person
221             obtaining a copy of this software and associated documentation
222             files (the "Software"), to deal in the Software without
223             restriction, including without limitation the rights to use,
224             copy, modify, merge, publish, distribute, sublicense, and/or sell
225             copies of the Software, and to permit persons to whom the
226             Software is furnished to do so, subject to the following
227             conditions:
228              
229             The above copyright notice and this permission notice shall be
230             included in all copies or substantial portions of the Software.
231              
232             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
233             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
234             OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
235             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
236             HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
237             WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
238             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
239             OTHER DEALINGS IN THE SOFTWARE.
240              
241             =cut
242              
243             # }}}1
244             # vi: set fdm=marker fdl=0: #