Last active
December 15, 2015 14:39
-
-
Save hitode909/5275596 to your computer and use it in GitHub Desktop.
PPI::Transformを使ってソースコード中のトークンを置換する
This file contains 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
use strict; | |
use warnings; | |
use Perl6::Say; | |
for my $i (0..10) { | |
if ($i % 2 == 0) { | |
say "$i: Even"; | |
} else { | |
say "$i: Odd"; | |
} | |
} |
This file contains 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
use strict; | |
use warnings; | |
use Perl6::Say; | |
for my $i (0..10) { | |
unless ($i % 2 == 0) { | |
say "$i: Even"; | |
} else { | |
say "$i: Odd"; | |
} | |
} |
This file contains 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
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