App-Greple
view release on metacpan or search on metacpan
script/greple view on Meta::CPAN
$count_need = $posi - $1;
}
elsif (/^\+(\d+)$/) { # --need +n
$count_need += $1;
}
elsif (/^(\d+)$/) { # --need n
$count_need = $1 - $must;
}
else {
die "$_ is not valid count.\n"
}
}
$count_allow = 0;
for (@opt_allow) {
if (/^-(\d+)$/) { # --allow -n
$count_allow = $nega - $1;
}
elsif (/^\+(\d+)$/) { # --allow +n
$count_allow += $1;
}
elsif (/^(\d+)$/) { # --allow n
$count_allow = $1;
}
else {
die "$_ is not valid count.\n"
}
}
}
##
## --matchcount
##
my $count_match_sub = sub {
local $_ = shift or return;
/[^\d,]/ and die "$_ is not valid count.\n";
my @c = map { $_ eq '' ? 0 : int } split(/,/, $_, -1);
if (@c == 1) {
return sub { $_[0] == $c[0] };
}
push @c, -1 if @c % 2;
return sub {
my @n = @c;
while (my($min, $max) = splice(@n, 0, 2)) {
return 0 if $_[0] < $min;
return 1 if $max <= 0 || $_[0] <= $max;
}
return 0;
}
}->($opt_matchcount);
##
## setup input/output filter
##
my $filter_d = App::Greple::Filter->new->parse(@opt_if);
unless ($opt_noif) {
$filter_d->append(
[ sub { s/\.Z$// }, 'zcat' ],
[ sub { s/\.g?z$// }, 'gunzip -c' ],
[ sub { m/\.pdf$/i }, 'pdftotext -nopgbrk - -' ],
[ sub { s/\.gpg$// }, 'gpg --quiet --no-mdc-warning --decrypt' ],
);
}
##------------------------------------------------------------
## miscellaneous setups
##
my @argv_files;
my $start_directory;
my $need_filename = ($opt_H or $opt_l);
my $current_file;
if (@opt_chdir) {
$start_directory = getcwd;
@opt_chdir = uniq(map { glob $_ } @opt_chdir);
push @argv_files, splice(@ARGV);
unless ($opt_h or
(@opt_chdir == 1 and @argv_files == 1 and @opt_glob == 0)) {
$need_filename++;
}
}
elsif (@opt_glob) {
push @ARGV, map(glob, @opt_glob);
}
push(@ARGV, '-') unless @ARGV || @argv_files || @opt_glob || $opt_readlist;
if ((@ARGV > 1 or $opt_readlist) and not $opt_h) {
$need_filename++;
}
$opt_filestyle = 'none' if not $need_filename;
$opt_join = 1 if $opt_joinby ne "";
##------------------------------------------------------------
## colors
##
our %colormap = (
FILE => "G",
LINE => "Y",
BLOCK => "B",
TEXT => "",
BLOCKEND => "/WE",
PROGRESS => "B",
TOP => "",
MIDDLE => "",
BOTTOM => "",
);
our @colors;
use Getopt::EX::Colormap;
my $color_handler = Getopt::EX::Colormap
->new(HASH => \%colormap, LIST => \@colors)
->load_params(@opt_colormap);
my @default_color =
$opt_ansicolor eq '16'
? qw(RD GD BD CD MD YD)
: qw(000D/544 000D/454 000D/445
script/greple view on Meta::CPAN
=over 7
=item B<--icode>=I<code>
Target file is assumed to be encoded in utf8 by default. Use this
option to set specific encoding. When handling Japanese text, you may
choose from 7bit-jis (jis), euc-jp or shiftjis (sjis). Multiple code
can be supplied using multiple option or combined code names with
space or comma, then file encoding is guessed from those code sets.
Use encoding name C<guess> for automatic recognition from default code
list which is euc-jp and 7bit-jis. Following commands are all
equivalent.
greple --icode=guess ...
greple --icode=euc-jp,7bit-jis ...
greple --icode=euc-jp --icode=7bit-jis ...
Default code set are always included suspect code list. If you have
just one code adding to suspect list, put + mark before the code name.
Next example does automatic code detection from euc-kr, ascii, utf8
and UTF-16/32.
greple --icode=+euc-kr ...
If the string "B<binary>" is given as encoding name, no character
encoding is expected and all files are processed as binary data.
=item B<--ocode>=I<code>
Specify output code. Default is utf8.
=back
=head2 FILTER
=over 7
=item B<--if>=I<filter>, B<--if>=I<EXP>:I<filter>
You can specify filter command which is applied to each file before
search. If only one filter command is specified, it is applied to all
files. If filter information include colon, first field will be perl
expression to check the filename saved in variable $_. If it
successes, next filter command is pushed.
greple --if=rev perg
greple --if='/\.tar$/:tar tvf -'
If the command doesn't accept standard input as processing data, you
may be able to use special device:
greple --if='nm /dev/stdin' crypt /usr/lib/lib*
Filters for compressed and gzipped file is set by default unless
C<--noif> option is given. Default action is like this:
greple --if='s/\.Z$//:zcat' --if='s/\.g?z$//:gunzip -c'
File with C<.gpg> suffix is filtered by B<gpg> command. In that case,
pass-phrase is asked for each file. If you want to input pass-phrase
only once to find from multiple files, use C<-Mpgp> module.
If the filter starts with C<&>, perl subroutine is called instead of
external command. You can define the subroutine in F<.greplerc> or
modules. B<Greple> simply call the subroutine, so it should be
responsible for process control. It may have to use C<POSIX::_exit()>
to avoid executing an C<END> block on exit or calling destructor on
the object.
=item B<--noif>
Disable default input filter. Which means compressed files will not
be decompressed automatically.
=item B<--of>=I<filter>
=item B<--of>=I<&func>
Specify output filter which process the output of B<greple> command.
Filter command can be specified in multiple times, and they are
invoked for each file to be processed. So next command reset the line
number for each file.
greple --of 'cat -n' string file1 file2 ...
If the filter starts with C<&>, perl subroutine is called instead of
external command. You can define the subroutine in F<.greplerc> or
modules.
Output filter command is executed only when matched string exists to
avoid invoking many unnecessary processes. No effect for option
C<-l> and C<-c>.
=item B<--pf>=I<filter>
=item B<--pf>=I<&func>
Similar to C<--of> filter but invoked just once and takes care of
entire output from B<greple> command.
=back
=head2 RUNTIME FUNCTIONS
=over 7
=item B<--begin>=I<function>(I<...>)
=item B<--begin>=I<function>=I<...>
Option C<--begin> specify the function executed at the beginning of
each file processing. This I<function> have to be called from B<main>
package. So if you define the function in the module package, use the
full package name or export properly.
If the function dies with a message starting with a word "SKIP"
(C</^SKIP/i>), that file is simply skipped. So you can control if the
script/greple view on Meta::CPAN
You can use the module like this:
greple -Mperl --pod default greple
greple -Mperl --colorful --code --comment --pod default greple
If special subroutine C<initialize()> and C<finalize()> are defined in
the module, they are called at the beginning with
L<Getopt::EX::Module> object as a first argument. Second argument is
the reference to C<@ARGV>, and you can modify actual C<@ARGV> using
it. See L<App::Greple::find> module as an example.
Calling sequence is like this. See L<Getopt::EX::Module> for detail.
1) Call initialize()
2) Call function given in -Mmod::func() style
3) Call finalize()
=head1 HISTORY
Most capability of B<greple> is derived from B<mg> command, which has
been developing from early 1990's by the same author. Because modern
standard B<grep> family command becomes to have similar capabilities,
it is a time to clean up entire functionalities, totally remodel the
option interfaces, and change the command name. (2013.11)
=head1 SEE ALSO
L<grep(1)>, L<perl(1)>
L<App::Greple>, L<App::Greple::Grep>
L<https://github.com/kaz-utashiro/greple>
L<Getopt::EX>, L<https://github.com/kaz-utashiro/Getopt-EX>
=head1 AUTHOR
Kazumasa Utashiro
=head1 LICENSE
Copyright 1991-2026 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# LocalWords: greple egrep foo baz yabba dabba doo ascii greplerc
# LocalWords: regex readlist iname jpg jpeg gif png tbz tgz pdf RGB
# LocalWords: perlre fgrep grep perl joinby KATAKANA InKatakana utf
# LocalWords: nonewline filestyle linestyle chdir mtime nocolor jis
# LocalWords: STDOUT colormap Cyan BLOCKEND LESSANSIENDCHARS setuid
# LocalWords: sprintf regioncolor uniqcolor ansicolor nocolorful jp
# LocalWords: struct sockaddr blockend icode euc shiftjis sjis zcat
# LocalWords: ocode gunzip gpg FILELABEL substr eval misc unicode
# LocalWords: GREPLEOPTS shellwords Katakana builtin pgp autoload
# LocalWords: ENV App ARGV mg Kazumasa Utashiro github colorindex
# LocalWords: matchcount gzipped stdin func CPANMINUS cpanm kana
( run in 1.030 second using v1.01-cache-2.11-cpan-df04353d9ac )