Hello all. Recently a friend asked me to write a script to do the following:

1. user inputs start month/day, end month/day;
2. randomly choose breakfast, lunch, dinner every day during that span.
3. save to file

I won't bore you with why he wanted this, but here's what I came up with:

#!/usr/bin/perl use warnings; use strict; my %months = ( 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "M +ay", 6 => "June", 7 => "July", 8 => "Aug", 9 => "Sept", 10 => "Oct", +11 => "Nov", 12 => "Dec" ); my @breakfast = qw/bagel cereal toast yogurt/; my @lunch = qw/sandwich milkshake pb&j hoagie/; my @dinner = qw/pasta rice chicken steak/; my $file = ''; my ($start_day, $start_mon, $end_day, $end_mon); my ($total, $len, $curr_mon, $curr_day); my $three_one = join ('|', qw/1 3 5 7 8 10 12/); #months with 31 days my $three_zero = join ('|', qw/4 6 9 11/); #months with 30 days print "Enter start month (ie., for January, enter 1): "; chomp($start_mon = <STDIN>); print "Enter start day (ie., 20): "; chomp($start_day = <STDIN>); print "Enter end month (ie., for March, enter a 3): "; chomp($end_mon = <STDIN>); print "Enter end day (ie., 21): "; chomp($end_day = <STDIN>); print "Save to file [hit enter for default 'plan']: "; chomp($file = <STDIN>); if($file eq '') { $file = "plan"; } open(FH, ">$file") or die "open: $!\n"; $curr_mon = $start_mon; $curr_day = $start_day; $total = get_days(); # get # of days for the current month while (1) { last if($curr_mon eq $end_mon && $curr_day == $end_day + 1); print FH "$months{$curr_mon} $curr_day\n"; $len = length($months{$curr_mon}) + length($curr_day) + 1; print FH "-" x $len, "\n"; print FH "Breakfast: ", $breakfast[ rand @breakfast], "\n"; print FH "Lunch : ", $lunch[ rand @lunch], "\n"; print FH "Dinner : ", $dinner[ rand @dinner], "\n"; print FH "-" x $len, "\n"; $curr_day++; if($curr_day > $total) { $curr_mon++; $curr_day = 1; $total = get_days(); } } print "Written to file $file.\n"; close(FH); sub get_days { if($curr_mon =~ /$three_one/) { return 31; } elsif($curr_mon =~ /$three_zero/) { return 30; } elsif($curr_mon == 2) { return 29; } else { die "Bad month entered!\n"; } }

It works fine - it just seems long and not very neat. I'm still a beginner and was wondering how more experienced monks might have written it.


In reply to Critique by phenom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.