The things we do when we are in love!

Recently I saw a pretty postcard in a shop which had a list of dates on it as follows:

I love you on the 1st of January 2011 I love you on the 2nd of January 2011 I love you on the 3rd of January 2011 ... I love you on the 31st of December 2011
And it seemed a nice idea to send such a message by e-mail to my loved one.

Now I am a terrible typist and I am sure I will type many errors in such e-mail, so why not writing a script that does it for me?

Here is what I wrote:

# every day I love you use Modern::Perl; use Date::Calc::Iterator; use Lingua::EN::Inflect qw/ORD/; my @months = qw /January February March April May June July August September Octo +ber November December/; my $start = $ARGV[0] ? parse_date( $ARGV[0] ) : [ (localtime)[5] + 1900, (localt +ime)[4] + 1, (localtime)[3] ]; my $end = $ARGV[1] ? parse_date( $ARGV[1] ) : [ $start->[0] + 1, $star +t->[1], $start->[2] ]; my $i = Date::Calc::Iterator->new( from => $start, to => $end ); my @result; my $day; push @result, join ' ', 'I love you on the', ORD( $day->[2] ), 'of', $months[ $day->[1] - 1 +], "$day->[0]." while $day = $i->next; pop @result unless $ARGV[1]; # drop the last one if the end is calc +ulated by default say for @result; sub parse_date { my $date = shift; return undef unless $date; if ( $date =~ m/^(\d{2})[\/-]?(\d{2})[\/-]?(\d{4})$/ ) { return [ $3, $2, $1 + 0 ]; } else { return undef; } }
Usage is perl everyday.pl start_date end_date Dates are in the ddmmyyyy, dd-mm-yyyy or dd/mm/yyyy format. If you leave out end_date it will default to one year after start_date. If you also leave out start_date, it will default to today.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to "I love you every day"-script by CountZero

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.