Before I took up Perl, I was learning Java. I was looking through the Oracle Java Programming forums, and I saw a link to an article that said the best type of perl code is "dumb code", or code that is very straightforward and almost spells out what you're doing. I was wondering, dumb code is good for Java, but what is the best type of perl code? Consider the following code:
#!/usr/bin/perl -w use strict; use warnings; print "I will calculate whatever\n"; print "you specify according to\n"; print "Ohm's law. What shall I\n"; print "calculate? Type either\n"; print "voltage, current, or\n"; print "resistance.\n"; my $i = 0; while($i == 0) { my $calculation = <STDIN>; chomp $calculation; if($calculation =~ m/voltage/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = <STDIN>; chomp $current; print "Now type the resistance of the circuit.\n"; my $resistance = <STDIN>; chomp $resistance; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $voltage = $current * $resistance; print "Ohm's law is V = IR, so the voltage of\n"; print "the circuit is $voltage volts.\n"; $i++; } elsif($calculation =~ m/current/) { print "Type the resistance of the circuit.\n"; my $resistance = <STDIN>; chomp $resistance; print "Now type the voltage of the circuit.\n"; my $voltage = <STDIN>; chomp $voltage; my $current = $voltage / $resistance; chomp $current; my $otherCurrent = $current / 1000; chomp $otherCurrent; print "Ohm's law is V = IR, so the current of\n"; print "the circuit is $current amps,\n"; print "or $otherCurrent milliAmps.\n"; $i++; } elsif($calculation =~ m/resistance/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = <STDIN>; chomp $current; print "Now type the voltage of the circuit.\n"; my $voltage = <STDIN>; chomp $voltage; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $resistance = $voltage / $current; chomp $resistance; print "Ohm's law is V = IR, so the resistance of\n"; print "the circuit is $resistance ohms.\n"; $i++; } else { print "That is not a valid answer.\n"; print "Retype your answer.\n"; } }
This is an example of dumb code for me. It calculates voltage, current, or resistance according to Ohm's Law, or the equation Voltage = Current * Resistance. It pretty much is self explanatory for the most part, except for maybe the regexes. Is this good perl code or bad perl code? What makes it good or bad code?

In reply to What makes good Perl code? by slinky773

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.