Just my 2 cents, I'm sure it can be improved a lot. I simply found that is more readable like this... and at least is a little more compact.
#!/usr/bin/perl -w use strict; use warnings; use feature qw(say); use English qw(-no_match_vars); BEGIN { $^W = 1; } sub BEGIN { if ($^W == 1) {say "Let's get started..."} else {die $@} } local $OUTPUT_AUTOFLUSH = 1; local $OUTPUT_RECORD_SEPARATOR = *\ ; print <<NOTE_ONE; I will calculate whatever you specify according to Ohm's law. What shall I calculate? Type either voltage, current, or resistance NOTE_ONE my $type_current = <<NOTE_TWO; Type the current of the circuit. Add mA at the end of your answer, if your answer is in milliAmps: NOTE_TWO my $type_voltage = "Now type the voltage of the circuit:"; my $type_resistance = "Type the resistance of the circuit:"; my $ohmlaw = "Ohm's law is V = IR, so "; my $i = 0; while ($i == 0) { my $calculation = <STDIN>; if ($calculation =~ /voltage/) { say $type_current; my $current = <STDIN>; say $type_resistance; my $resistance = <STDIN>; if ($current =~ /mA/) { $current =~ s/mA//; $current =~ s/(^\s+| )//; $current /= 1000; } else {()} say $ohmlaw . "the voltage of\nthe circuit is " . $current * $ +resistance . " volts."; ++$i; } elsif ($calculation =~ /current/) { say $type_resistance; my $resistance = <STDIN>; say $type_voltage; my $voltage = <STDIN>; say $ohmlaw . "the current of\nthe circuit is ". $voltage / $r +esistance . " amps., or " . $voltage /($resistance*1000) . " milliAmp +s."; ++$i; } elsif ($calculation =~ /resistance/) { say $type_current; my $current = <STDIN>; say $type_voltage; my $voltage = <STDIN>; if ($current =~ /mA/) { $current =~ s/mA//; $current =~ s/(^\s+| )//; $current /= 1000; } else {()} say $ohmlaw ."the resistance of\nthe circuit is ". $voltage / +$current . "ohms."; ++$i; } else {say "That is not a valid answer.\nRetype your answer."} }

In reply to Re: What makes good Perl code? by pvaldes
in thread 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.