#$/usr/bin/perl -w
use strict;
use warnings;
my ($date, $month, $year, $weekday) = (localtime)[3,4,5,6];
$month++; # Converts $month to human readable.
my $days = MonthDays($month, $year); # Figures out how many
+ $days are in current month.
my $offset = ($date % 7); # Returns the offset of weekd
+ays today is from the 1st, 0-6.
# i.e. 0 means that today's $weekday == th
+e 1st's $weekday.
my @months = qw(January February March April May June July August Sept
+ember October November December);
my $i = 1; # Set increment counter to 1.
$month--; # Converted $month back to machine-re
+adable.
$month = $months[$month]; # Converts $month number to m
+onth Name
$year = $year + 1900; # Converts $year to human rea
+dable
$weekday = ($weekday - $offset);
my @weekdays = qw(Monday Tuesday Wednesday Thursday Friday Saturday Su
+nday);
######################################################################
+###########################
# #
# This section names the file - either journal.txt or journal_m
+onth_year.txt #
# #
# #
######################################################################
+###########################
my $file = "journal.txt";
if (-e "journal.txt") { # Checks to see if journal.txt exi
+sts.
$file = ("journal_" . $month . "_" . $year . ".txt"); # If so, gi
+ves it a dated name instead.
}
if (-e $file) {
die "File $file already exists!\n";
sleep(2); # I sleep here so I can read the msg in W
+in32.
}
######################################################################
+###########################
# #
# OK, now're we're ready to get on with things. This part actua
+lly outputs #
# the file in a simple, nicely formatted monthly journal.
+ #
# #
######################################################################
+###########################
open (OUTPUT, "> $file")
or die "Could not open file for writing: $!\n";
while ($i <= $days) {
print OUTPUT "$weekdays[$weekday], $month $i, $year\n\n\n";
$i++;
push(@weekdays, shift(@weekdays));
}
close (OUTPUT);
sub MonthDays { # This subroutine determines the natura
+l weekday names.
my @monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 );
my $month = shift(@_);
my $year= @_ ? shift(@_) : 1900+(localtime())[5];
if( $year <= 1752 ) {
# Note: Although September 1752 only had 19 days,
# they were numbered 1,2,14..30!
return 19 if 1752 == $year && 9 == $month;
return 29 if 2 == $month && 0 == $year % 4;
} else {
return 29 if 2 == $month and
0 == $year%4 && 0 == $year%100 || 0 == $year%400;
}
return $monthDays[$month-1];
}
-
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.