in reply to What makes good Perl code?
Update: made a few additions.#!/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"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What makes good Perl code?
by jwkrahn (Abbot) on Aug 16, 2011 at 04:40 UTC |