/** * The HelloWorldApp Class impliments an application that displays "Hello World!" to standard output */ public class HelloWorldApp { public static void main (String[] args) { // Display "Hello World!" System.out.println("Hello World!"); // end of line comment System.out.println("Hello World!"); /* end of line comment */ System.out.println("Hello World!/*"); /* end of line comment */ /* this is a multi line comment */ /* another comment */ // /* is this a valid comment /* // */ System.out.println("//Hello World!/*"); // is /* this valid } } #### #!/usr/bin/perl use strict; my $infile = "HelloWorldApp.java"; my %linehash; my $linecounter =0; my $comment_started = 0; my $multiline_comment; open in, "$infile" or die "could not open $infile\n"; while () { ## you may need to get creative in matching comments ## because java allows some fun combinations - see HelloWorld ## ## grab single line comments first else look for multiline if (/\/\/.*\n/ || /\/\*.*\*\//) { $linehash{$linecounter} = $_; } else { ## possible multiline comment start if ($comment_started) { $multiline_comment = $multiline_comment . $_; ## don't mess with $_ as later comparisons may need the newline in place chomp $multiline_comment; $multiline_comment = $multiline_comment . " "; ## end of multiline comment if (/\*\//) { $linehash{$comment_started} = $multiline_comment; $comment_started = 0; $multiline_comment = ""; } } ## start multiline comment if (/\/\*/) { $comment_started=$linecounter; } } $linecounter++; } my @keys = sort{$a <=> $b}(keys(%linehash)); for (@keys) { print "key=$_ value=$linehash{$_}"; }