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);
In reply to A place for code reviews by johnnywang
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |