Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Analize Java source file with perl?

    5 answers - 508 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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

    thank u.
  • No.1 | | 914 bytes | |

    I'm sure there's a better way to do this, but it really isn't too hard.

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $file = shift || die "Please provide a java file to check.\n";
    my @lines = `cat $file`;

    foreach (@lines) {
    /class (\w+)/ && print "$1\n";
    }
    end

    10/26/06, bou, hou (GE Money, consultant) <hou.bou (AT) ge (DOT) comwrote:
    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
    >

    thank u.
  • No.2 | | 1453 bytes | |


    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

    What have you tried, and how has it failed to meet your expectations?

    You may want to try http://search.cpan.org/ to see if something is
    already written to get you started.

    The problem with any task of this nature is: To reliably parse a
    language in all (even pathological) cases requires a program equal in
    complexity to that of the compiler itself[1].

    That being said, a simple heuristic approach can be quite successful
    for non-pathological cases, or a code-base that follows some set of
    rules more stringent than those required by the standard.

    For example, if you can be 100% certain that class definitions will
    always begin with "\npublic class Classname {", then your job has
    gotten much simpler, but your program will fail (possibly
    spectacularly) on a java file that does not follow that rule.

    [1] More accurately, to the lexer and parser components of the
    compiler -- you can safely skip code generation.

    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    Lawrence Statton - lawrenabae (AT) abaluon (DOT) abaom s/aba/c/g
    Computer software consists of only two components: ones and
    zeros, in roughly equal proportions. All that is required is to
    sort them into the correct order.
  • No.3 | | 806 bytes | |

    10/26/06, bou, hou (GE Money, consultant) <hou.bou (AT) ge (DOT) comwrote:
    hello, all
    I want to get the Class name of .java file with perl ,

    I want to get like this
    >perl ClassChecker.pl AAA.java
    >ClassA
    >ClassB


    Quick and dirty:

    $ perl -n -e 'print "$1\n" if /class\s+(\w+)/' AAA.java
    ClassA
    ClassB

    This does not know anything about Java syntax and just looks for a
    word after 'class' and some space. If it is in comments or you have
    something like

    public class
    Foo {
    }

    it won't understand it.

    More robust solutions are feasible. For example, after improving the
    recent CPAN module Parse::Java, you can do a lot better. To quote
    Randal, "Learn or hire".
  • No.4 | | 1291 bytes | |

    From: "Andy Greenwood" <greenwood.andy (AT) gmail (DOT) com>
    I'm sure there's a better way to do this, but it really isn't too
    hard.

    #!/usr/bin/perl use warnings; use strict;

    my $file = shift || die "Please provide a java file to check.\n"; my
    @lines = `cat $file`;

    AUUUUUUUU!!!

    open my $IN, '<', $file or die "Can't open $file: $^E\n";
    @lines = <$IN>;
    close $IN;

    is much much more efficient and unlike the cat it's actually
    portable!

    foreach (@lines) {
    /class (\w+)/ && print "$1\n";
    }

    You do not actually need to load the whole file into an array and
    then loop through the array, this would be better:

    open my $IN, '<', $file or die "Can't open $file: $^E\n";
    while (<$IN>) {
    /class (\w+)/ && print "$1\n";
    }
    close $IN;

    course the problem with this is that it ignores comments and
    string literals and it assumes that there will always be a single
    space between the "class" and the classname.

    Jenda
    Jenda (AT) Krynicky (DOT) cz http://Jenda.Krynicky.cz
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery
  • No.5 | | 294 bytes | |

    schreef:
    my $text = do { local $/; <$fh};
    There is a problem with this idiom: it uses about twice the memory of
    this variant:
    my $text ; { local $/ ; $text = <$fh}
    So if your files can be big (related to the amount of memory available),
    use the latter.

Re: Analize Java source file with perl?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "Perl"

EMSDN.COM