Created
April 11, 2013 10:15
-
-
Save pmakholm/5362247 to your computer and use it in GitHub Desktop.
Simple command line interface for PPI (Perl Parsing Interface)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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