Skip to content

Instantly share code, notes, and snippets.

@hitode909
Last active Dec 15, 2015
Embed
What would you like to do?
PPI::Transformを使ってソースコード中のトークンを置換する
use strict;
use warnings;
use Perl6::Say;
for my $i (0..10) {
if ($i % 2 == 0) {
say "$i: Even";
} else {
say "$i: Odd";
}
}
use strict;
use warnings;
use Perl6::Say;
for my $i (0..10) {
unless ($i % 2 == 0) {
say "$i: Even";
} else {
say "$i: Odd";
}
}
use strict;
use warnings;
package PPI::Transform::ReplaceTokens {
use strict;
use warnings;
use Encode qw(encode_utf8 decode_utf8);
use parent qw(PPI::Transform);
sub document {
my ($self, $document) = @_;
my $changed = 0;
my $tokens = $document->find('PPI::Token');
for my $token (@$tokens) {
my $new_content = $self->{rules}->{decode_utf8 $token->content};
next unless defined $new_content;
$token->set_content(encode_utf8 $new_content);
$changed++;
}
$changed;
}
}
my $transform = PPI::Transform::ReplaceTokens->new(
rules => {
if => 'unless',
unless => 'if',
},
);
$transform->file('odd_even.pl' => 'out.pl');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment