Skip to content

Instantly share code, notes, and snippets.

@hitode909
Last active December 15, 2015 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/5275596 to your computer and use it in GitHub Desktop.
Save hitode909/5275596 to your computer and use it in GitHub Desktop.
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