in reply to What makes good Perl code?

I condensed it a bit. This is closer to how I would do it:
#!/usr/bin/perl -sl BEGIN { $^W = 1; } sub BEGIN { if ($^W == 1) { print "Let's get started...\n"; } else { die $@; } } use strict; use warnings; use English qw(-no_match_vars); local $OUTPUT_AUTOFLUSH = 1; local $OUTPUT_RECORD_SEPARATOR = *\ ; print "I will calculate whatever \n", "you specify according to\n", "Ohm's law. What shall I\n", "calculate? Type either\n ", "voltage, current, or \n", "resistance.\n"; my $i = 0; while ($i == 0) { my $calculation = <STDIN>; if ($calculation =~ /voltage/) { print "Type the current of the circuit.\n", "Add mA at the end of your answer\n", "if your answer is in milliAmps.\n"; chomp(my $current = <STDIN>); print "Now type the resistance of the circuit.\n"; chomp(my $resistance = <STDIN>); if ($current =~ /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 =~ /current/) { print "Type the resistance of the circuit.\n"; chomp(my $resistance = <STDIN>); print "Now type the voltage of the circuit.\n"; my $voltage = <STDIN>; my $current = $voltage / $resistance; my $otherCurrent = $current / 1000; print "Ohm's law is V = IR, so the current of\n", "the circuit is $current amps\n", "or $otherCurrent milliAmps.\n"; ++$i; } elsif ($calculation =~ /resistance/) { print "Type the current of the circuit.\n", "Add mA at the end of your answer\n", "if your answer is in milliAmps.\n"; chomp(my $current = <STDIN>); print "Now type the voltage of the circuit\n."; my $voltage = <STDIN>; if ($current =~ /mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { (); } my $resistance = $voltage / $current; print "Ohm's law is V = IR, so the resistance of\n", "the circuit is $resistance ohms.\n"; ++$i; } else { print "That is not a valid answer.\n" "Retype your answer.\n"; } }
Update: made a few additions.

Replies are listed 'Best First'.
Re^2: What makes good Perl code?
by jwkrahn (Abbot) on Aug 16, 2011 at 04:40 UTC
    #!/usr/bin/perl -sl

    The -s switch is used to parse command line switches but your program doesn't use command line switches.

    The -l switch is used to set the Output Record Separator to the same value as the Input Record Separator but later you are explicitly changing the Output Record Separator.

    BEGIN { $^W = 1; } sub BEGIN { if ($^W == 1) { print "Let's get started...\n"; } else { die $@; } }

    Why is this code here?    You turn global warnings on in a BEGIN block.    Why not just use the -w or -W switch?    And why use lexical warnings later as well?    And why die if warnings aren't enabled when you just turned them on?

    local $OUTPUT_RECORD_SEPARATOR = *\ ;

    Why are you assigning the value '*main::\' to $\?