Hi, I learnt perl by reading those perl books (camel,etc.). There has never been someone to look over my shoulders. I suspect there're many people like me, maybe a majority of perl programmers. In the back of my mind, I am always wondering how much of what I've been doing is not quite right, e.g., there is a better way. Although "there is more than one way to do it", I'm sure some of those are bad. PM is a great place, yet I don't feel comfortable posting a code example, and say "guys, please do a code review for me". Is there an appropriate node at PM for that?

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.