Skip to content

Instantly share code, notes, and snippets.

@goccy
Last active August 29, 2015 13:58
Show Gist options
  • Save goccy/10094493 to your computer and use it in GitHub Desktop.
Save goccy/10094493 to your computer and use it in GitHub Desktop.
Compiler::Parser::(AST|Node)::findの使い方
use strict;
use warnings;
use Compiler::Lexer;
use Compiler::Parser;
use Data::Dumper;
my $script = do { local $/; <DATA> };
my $tokens = Compiler::Lexer->new('-')->tokenize($script);
my $ast = Compiler::Parser->new->parse($tokens);
print Dumper +[
map {
+{
module_name => $_->data,
exported_names => ($_->args) ? [ map { $_->data } @{$_->args->find(node => 'Leaf')} ] : undef
};
} @{$ast->find(node => 'Module')}
];
=output
$VAR1 = [
{
'module_name' => 'A',
'exported_names' => undef
},
{
'module_name' => 'B',
'exported_names' => [
'exportA exportB'
]
},
{
'module_name' => 'C',
'exported_names' => [
'exportA',
'exportB'
]
}
];
=cut
__DATA__
use A;
require B qw/exportA exportB/;
use C ('exportA', 'exportB');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment