Just as an example, here's a piece of code I came up in the past few minutes since I'm tied of writing those java been getter/setters (and my old emacs function is seeing its limits). I just wanted an interactive way to create a java bean class, i.e., in stdout, enter "int,age" will add a bean property "age" with type "int" and its getter/setter, and enter a blank line to quit. How's the code? what can be improved, in terms of idioms,styles, etc.
#!/usr/bin/perl -w use strict; my %init_value = ('int'=>'0', 'long'=>'0l', ); print "package: "; my $package = <>; chomp $package; print "class: "; my $class = <>; chomp $class; #print the header open(OUTFILE, ">$class.java") or die "Can't open $class.java for writi +ng: $!"; print OUTFILE<<HEADER; /** * \$Source:\$ * \$Date: \$ * \$Revision: \$ * \$Author: \$ * **/ package $package; public class $class\{ HEADER my %map = (); print "Enter bean property (e.g. int,name\n"; my $line; do{ $line = <>; chomp $line; my($type,$name) = split(/,/,$line); if ($type && $name){ $map{$type} = $name; }else{ if($line){ print "Invalid: $line\n"; } } }while($line); # print out the fields, and init values foreach my $type ( sort keys %map ){ my $value = defined $init_value{$type} ? $init_value{$type} : "nul +l"; print OUTFILE "\tprivate $type _$map{$type} = $value;\n"; } #print out getters/setters foreach my $type (sort keys %map){ my $name = $map{$type}; my $method = ucfirst $name; print OUTFILE<<END; public $type get$method(){ return _$name; } public void set$method($type $name){ _$name = $name; } END } print OUTFILE "}\n"; close(OUTFILE);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A place for code reviews
by BUU (Prior) on Aug 30, 2004 at 20:35 UTC | |
|
Re: A place for code reviews
by ambrus (Abbot) on Aug 30, 2004 at 18:06 UTC | |
by johnnywang (Priest) on Aug 30, 2004 at 18:27 UTC | |
by ambrus (Abbot) on Aug 30, 2004 at 18:40 UTC | |
|
Re: A place for code reviews
by hv (Prior) on Aug 31, 2004 at 10:48 UTC | |
by demerphq (Chancellor) on Aug 31, 2004 at 16:23 UTC | |
|
Re: A place for code reviews
by greenFox (Vicar) on Aug 31, 2004 at 13:03 UTC | |
|
Re: A place for code reviews
by rickest (Novice) on Sep 03, 2004 at 07:04 UTC |