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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.