Skip to content

Instantly share code, notes, and snippets.

@jgrar
Created March 7, 2018 01:38
Show Gist options
  • Save jgrar/3d0555a6563591e7bc44ded430757722 to your computer and use it in GitHub Desktop.
Save jgrar/3d0555a6563591e7bc44ded430757722 to your computer and use it in GitHub Desktop.
Answer to Scripting Programming tutorial 2, question 2
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
=pod
=head1 Tutorial 2: Question 2
Write a Perl script to calculate the day of a given date (assuming the same
month). It randomly generates a date between 1-7 as the first Monday of
the month (using the rand function).
=over 4
=item 1.
If the user enters a date between 1-28, then the script will print out the
day.
=item 2.
If the date is in between 29-31, the corresponding day will still be
printed but with a warning such as "that date might not be valid in the
current month".
=item 3.
If the given number is larger than 31 or less than 1 an error will be
printed.
=back
=cut
#my $firstMonday = 5; # march 2018
my $firstMonday = 1 + int(rand(7));
my @days = (
"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"
);
my $date = shift;
if ($date < 1 || $date > 31) {
print "error: date out of bounds\n";
exit 1;
}
if ($date > 28) {
print "warning: that date might not be valid in the current month!\n";
}
print $days[ ($date - $firstMonday) % 7 ] . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment