Skip to content

Instantly share code, notes, and snippets.

@pmakholm
Created April 11, 2013 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmakholm/5362247 to your computer and use it in GitHub Desktop.
Save pmakholm/5362247 to your computer and use it in GitHub Desktop.
Simple command line interface for PPI (Perl Parsing Interface)
#!/usr/bin/env perl
use v5.12.0;
use Getopt::Long;
use File::Next;
use PPI;
my ($match, $content) = qw(Element content);
my ($count, $verbose);
GetOptions(
"match=s" => \$match,
"content=s" => \$content,
"count!" => \$count,
"verbose!" => \$verbose,
);
my $matches;
my $files = File::Next::files( @ARGV );
while(defined( my $file = $files->() )) {
next unless $file =~ /\.(?:pm|pl|t)$/;
my $doc = PPI::Document->new( $file, readonly => 1 )
or next;
for my $elem ( @{$doc->find($match) || []} ) {
$matches++;
next unless $elem->can($content);
say (($verbose ? "$file:" . $elem->line_number . ": " : ""), $elem->$content) unless $count;
}
}
say "$matches matches" if $count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment