challenge コメントの削除

ソースコードからコメント部分を削除するプログラム decomment を書いてください.
すくなくとも,decomment を記述したのと同じ言語で書かれているソースコードが
扱えるようにしてください.



Posted feedbacks - Perl

CPANモジュール PPI を利用して。
# SYNOPSISにまんまコメント削除の例が載ってたし
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use strict;
use warnings;

use PPI;

=head1 NAME

decomment - strip comments/pods from code

=head1 SYNOPSIS

  decomment.pl <some.pl>

=head1 SEE ALSO

L<http://ja.doukaku.org/185/>

PPI L<http://search.cpan.org/~adamk/PPI-1.203/lib/PPI.pm>

=cut

if ( !$ARGV[0] ) {
  print "usage: $0 <some.pl>\n";
  exit 0;
}
# test data
my $testdata = "string with # :)"; # this is a comment;

# use PPI to strip comment from code
my $doc = PPI::Document->new($ARGV[0]);
$doc->prune(q/PPI::Token::Pod/);
$doc->prune(q/PPI::Token::Comment/);
print $doc->content;

__END__
this section is not a comment.

Index

Feed

Other

Link

Pathtraq

loading...