Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Auto Fill

by golux (Chaplain)
on May 16, 2015 at 14:36 UTC ( #1126861=note: print w/replies, xml ) Need Help??

in reply to Auto Fill

Just one suggestion about your code. When I see something duplicated like this:
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();
it immediately makes me want to encapsulate the duplicated code into a subroutine, so as to follow the doctrine of DRY.

Here's one way you could rewrite it:

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");
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.

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.

say  substr+lc crypt(qw $i3 SI$),4,5

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1126861]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2020-11-24 21:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found