Subject: | Helper methods I'm using |
As requested, here are the helper methods I'm currently using in PPI::Analyze (coming to a CPAN mirror near you! ... when I get the time). Some of them could be rewritten to use $doc->find() instead of xpmatch() and they're not terribly robust (see. no error handling), but here you go:
sub PPI::Element::xpmatch {
my($doc, $path) = @_;
return PPI::XPath->new($doc)->match($path);
}
sub PPI::Element::is_var {
return $_[0]->class eq 'Token::Symbol';
}
sub PPI::Element::is_sub {
my $n = shift;
return 1
if $n->class eq 'Statement::Sub';
return 1
if $n->is_anon_sub;
}
## it'd be nice if anon subs were Statement::Sub instead of Structure::Block ...
sub PPI::Element::is_anon_sub {
return $_[0]->class eq 'Structure::Block'
and $_[0]->sprevious_sibling->content eq 'sub';
}
sub PPI::Element::is_in_sub {
my $n = shift;
$n->is_sub and return 1
while $n = $n->parent;
return;
}
sub PPI::Document::subs {
return $_[0]->xpmatch('//Statement::Sub'),
grep $_->is_anon_sub, $_[0]->xpmatch('//Structure::Block');
}
sub PPI::Document::vars {
return uniq $_[0]->xpmatch('//Token::Symbol');
}