Re: Analize Java source file with perl?
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 26 Oct 2006 06:29:49 -0700
Hou Bou wrote:
hello, all
I want to get the Class name of .java file with perl ,
How can I do it ? I think it is difficult to result the java comment .
For Example
a java file named AAA.java
--------------------
/* author : John Smith */
// comment
public class ClassA {
/* */
//comment
public static void main(String[] args) {
}
...
}
class ClassB {
...
}
---------------------
I want to get like this
perl ClassChecker.pl AAA.java
ClassA
ClassB
Use the Regexp::Common module to strip comments from the file, and then
parse it looking for class declarations. The below won't work for all
situations (at the very least, a string that contains what looks like a
class declaration will cause false positives) but will probably get
most. It is, however, completely untested.
#!/usr/bin/perl
use strict;
use warnings;
#download these two from CPAN
use Regexp::Common qw/comment/;
use File::Slurp;
my $file = shift or die "Usage: $0 filename.java\n";
my $contents = read_file($file);
$contents =~ s/$RE{comment}{Java}//g;
while ($contents =~ /\bclass\s+(\w+)\s*{/) {
print "$1\n";
}
__END__
.
- References:
- Analize Java source file with perl?
- From: Hou Bou
- Analize Java source file with perl?
- Prev by Date: Re: Analize Java source file with perl?
- Next by Date: Re: Analize Java source file with perl?
- Previous by thread: Re: Analize Java source file with perl?
- Next by thread: Re: Analize Java source file with perl?
- Index(es):
Relevant Pages
|
|