in reply to Auto Fill
it immediately makes me want to encapsulate the duplicated code into a subroutine, so as to follow the doctrine of DRY.print "==============\n\n"; print "| COMMODITY: |\n\n"; print "==============\n\n"; my $cmmdty = <STDIN>; $cmmdty = <STDIN> until defined $cmmdty; chomp $cmmdty; cls(); print "====================\n\n"; print "| LOADED OR EMPTY: |\n\n"; print "====================\n\n"; my $lore = <STDIN>; $lore = <STDIN> until defined $lore; chomp $lore; cls();
Here's one way you could rewrite it:
If you do this, you've got a subroutine "prompt_for_value()" which you can use for other input. If there's a bug in your subroutine, or you want to make an improvement in it (like changing "\n\n" to a single newline, for instance), you only have to make the change once within the subroutine and all callers of the subroutine will now do the same thing.sub prompt_for_value { my ($label) = @_; my $len = length($label); my $line = "=" x ($len + 5); # Display the prompt, eg. # # print "==============\n\n"; # print "| COMMODITY: |\n\n"; # print "==============\n\n"; print "$line\n\n"; print "| $label: |\n\n"; print "$line\n\n"; my $value = ""; while (1) { chomp(my $value = <STDIN>); if ($value) { cls(); # Is this subroutine defined? return $value; } } } my $cmmdty = prompt_for_value("COMMODITY"); my $lore = prompt_for_value("LOADED OR EMPTY");
Note that the prompt takes the length of the label into account, so the containing box is the right size.
This kind of code reuse will really help your programming skills.
|
|---|