Here are a few snippets you can use as examples for "shortening" your code. Whether it improves your code is something you'll have to decide for yourself. Golfed down code isn't necessary more legible or readable.
#hash slice with a range
my %months;
@months{1..12} = qw/Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec/;
You could write your "get_days()" function like this:
sub get_days {
my $month = shift;
return 31 if grep { $month eq $_ } qw/1 3 5 7 8 10 12/;
return 30 if grep { $month eq $_ } qw/4 6 9 11/;
return 29 if $month eq '2';
die "Bad month entered!\n";
}
This method eliminates the use of global variables that intentionally leak into functions.
For more speed-efficient lookup tables hashes are preferable, but these are small lists, and you're probably not checking thousands of times a second, so grep is probably fine. Not sure why I chose to treat month numbers as strings. I guess because I usually think of hash keys as strings too.
Those are the biggest changes I can think of, and they're really not necessary. Just look at them as "Another Way To Do It"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.