cbingel has asked for the wisdom of the Perl Monks concerning the following question:

All of my perl knowledge is self-taught through O'Reilly books and the Internet. Oftentimes I've been told that my code is ugly, or not well-formed, while it looks perfectly functional to me. Is there a reference somewhere, or a book I should read that would help me write better or cleaner code. I really don't want to write any more fodder for http://thedailywtf.com Edit: Someone asked for a sample, so here's a widget for my current project:
#!c:\perl\bin\perl.exe -w use strict; use Crypt::Twofish_PP; my $crypt = Crypt::Twofish_PP->new("innatelyinsecure"); if (!($ARGV[0])) { die "Syntax: crypt.pl (-e|-d) Text"; } elsif ($ARGV[2]) { die "incorrect syntax"; } elsif (!($ARGV[0] =~ /^-/)) { die "first argument must be mode. (-e or -d)"; } else { if ($ARGV[0] =~ /^-e$/) { my $result = $crypt->encrypt($ARGV[1]); print $result; }elsif ($ARGV[0] =~ /^-d$/) { my $result = $crypt->decrypt($ARGV[1]); print $result; }else{ print "Invalid mode argument (must be -e or -d)"; } }

Replies are listed 'Best First'.
Re: Good code reference
by cog (Parson) on May 11, 2005 at 13:42 UTC
Re: Good code reference
by reasonablekeith (Deacon) on May 11, 2005 at 14:20 UTC
    why note post some offending code so we can pick it apart?

    There's nothing programers like more than picking out faults in someone else's program. :-)

    update: There is a serious point here. You can religiously follow all the commandments in perlstyle, and still write shocking code. Tortured logic replaced by standard idioms can make for much easier to read code.

    ---
    my name's not Keith, and I'm not reasonable.
Re: Good code reference
by Kevad (Scribe) on May 11, 2005 at 13:50 UTC
    To help with existing code, check out PerlTidy.
Re: Good code reference
by Transient (Hermit) on May 11, 2005 at 13:46 UTC
Re: Good code reference
by sh1tn (Priest) on May 11, 2005 at 13:47 UTC
Re: Good code reference
by DrWhy (Chaplain) on May 12, 2005 at 21:08 UTC
    This doesn't look *too* bad to me. There are a few style deviations (I won't say violations since most of these standard are not rules per se). You only indent two spaces. The Perl standard is 4 spaces (personally I prefer 3 spaces, but that's just me). You're 2 space indents don't seem quite enough to sufficiently mark the indented blocks. if (!($ARGV[0])) { would be better written without the extranous do-nothing parentheses: if (! $ARGV[0]) {. This code } elsif (!($ARGV[0] =~ /^-/)) { is unnecessarily complex. This is better: } elsif ($ARGV[0] !~ /^-/) {. If you use the perl -s switch in your #! line you could simplify the logical structure of your program considerably since that will handle processing the command line option for you. Also all those if/elsif/else blocks tend to cover up the real core functionality of your program. There are an array of other syntactic tools in Perl that would let your code be clearer and shorter. Rather than go through all of them, let me just show you another way to approach the same problems that gives much clearer results:
    #!c:\perl\bin\perl.exe -ws use strict; use Crypt::Twofish_PP; die "Syntax: crypt.pl (-e|-d) Text" unless @ARGV == 1; my $crypt = Crypt::Twofish_PP->new('innatelyinsecure'); print $e ? $crypt->encrypt(shift @ARGV) : $d ? $crypt->decrypt(shift @ARGV) : "Invalid mode argument (must be -e or -d)";

    Now, if you read about the way that perl -s works you'll see that my version behaves a little differently than yours; it's not as tight with the command line. You could have multiple options listed even things that are not -d or -e.

    IMO there's nothing really bad in your code, but you are definitely not using the full range of expressive and functionaly power in Perl to create code that is optimally clear to a human reader.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."