in reply to What makes good Perl code?

Well, I updated the code a little bit, getting rid of the $i == 0 while loop and other stuff. But don't take my word for it: look at the 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 $calculation = 1; while($calculation) { $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: if($current && $resistance) { if($current =~ m/mA/) { $current =~ s/mA//; $current /= 1000; } else { } } else { print "Those aren't valid answers.\n"; print "Please type that again, will ya?\n"; 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"; $current = <STDIN>; chomp $current; print "Now type the resistance of the circuit.\n"; $resistance = <STDIN>; chomp $resistance; goto IF; } my $voltage = $current * $resistance; print "Ohm's law is V = IR, so the voltage of\n"; print "the circuit is $voltage volts.\n"; $calculation = 0; } 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; IF: if($resistance && $voltage) { } else { print "That is not a valid answer.\n"; print "Please type that again, will ya?\n"; print "Type the voltage of the circuit.\n"; $resistance = <STDIN>; chomp $resistance; print "Now type the voltage of the circuit.\n"; $voltage = <STDIN>; chomp $voltage; goto IF; } my $current = $voltage / $resistance; my $otherCurrent = $current / 1000; print "Ohm's law is V = IR, so the current of\n"; print "the circuit is $current amps,\n"; print "or $otherCurrent milliAmps.\n"; $calculation = 0; } 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: if($current && $voltage) { if($current =~ m/mA/) { $current =~ s/mA//; $current /= 1000; } else { } } else { print "Those aren't valid answers.\n"; print "Please type that again, will ya?\n"; 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"; $current = <STDIN>; chomp $current; print "Now type the voltage of the circuit.\n"; $voltage = <STDIN>; chomp $voltage; goto IF; } my $resistance = $voltage / $current; print "Ohm's law is V = IR, so the resistance of\n"; print "the circuit is $resistance ohms.\n"; $calculation = 0; } else { print "That is not a valid answer.\n"; print "Retype your answer.\n"; } }

Replies are listed 'Best First'.
Re^2: What makes good Perl code?
by slinky773 (Sexton) on Aug 16, 2011 at 16:01 UTC
    I was thinking about what Marshall said about user input, and how the program should be tolerant of user input no matter what it is. I was looking for a way to change this:
    IF: if($current && $resistance) { if($current =~ s/mA//i) { $current /= 1000; } else { } } else { print "Those aren't valid answers.\n"; print "Please type that again, will ya?\n"; 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"; $current = <STDIN>; chomp $current; print "Now type the resistance of the circuit.\n"; $resistance = <STDIN>; chomp $resistance; goto IF; }
    I want a way for the else block to run if both the inputs don't have numbers in them. Perhaps with an elsif block? However, I'm not aware of anything that would do what I require. Maybe with an algorithm? I don't know of ANY algorithms at all except for the Fischer-Yates shuffle, and that's pretty useless nowadays, considering Perl has a shuffle command anyways. Anyone know of a way to test for a number in a variable?

      One thing I do is to cleanup bulky lines after I get it working. For example, I might use $LOOP = $LOOP +1; until I get comfy and then go back and change that to ++$LOOP;.

      In this case, the first thing I noticed was chomp: chomp (my $current = <STDIN>);.

      Also, I keep the input and descriptor together.

      print "Type the current of the circuit.\n"; chomp (my $current = <STDIN>); print "Now type the resistance of the circuit.\n"; chomp (my $resistance = <STDIN>);

      But, really, as long as it's legible and someone can pick this up after you've moved on and edit/use it, you're a step above some people. Keep It Simple, =!Sloppy.

Re^2: What makes good Perl code?
by slinky773 (Sexton) on Aug 16, 2011 at 15:52 UTC
    I also condensed the if($current =~ m/mA/) stuff into this:
    if($current =~ s/mA//i) { $current /= 1000; }
    and put that everywhere it was in use.