To take advantage of Perl's wonderful syntactic shortcuts, you might try something like:
use strict; my ($day, $month, $year) = (localtime) [3, 4, 5]; $year += ($month += ($day = $day > 15 ? 0 : 15) ? 11 : 0) == 11 ? 1899 + : 1900; my ($start_day, $end_day) = map {sprintf("%4.4d%2.2d%2.2d", $year, $mo +nth % 12 + 1, $_)} ($day, $day ? 31 : 15); print "New Releases from $start_day to $end_day \n"
This makes heavy use of the conditional operator (?:) to back the date up to the beginning of the previous half-month, meanwhile putting the year in the proper century. Then map is used to generate the similarly-formatted $start_day and $end_day, while also correcting the month value. Note that to subtract one from the month, we are adding 11, and that the month correction takes this value modulo 12 before doing the final increment. Also, no account is made of months shorter than 31 days. Since this is used only in an expression for comparing real dates from a file, it shouldn't matter.

Of course, Perl's shortcuts sometimes do make code hard to read, and I may have crossed the line somewhat into obfuscation territory to make my point. But used carefully, they support a brevity that can actually foster comprehension by stripping a script of redundancy.

As with any code you get from the Monks, "try it before you buy it". I may have overlooked something. :-)


In reply to Re: date span in YYYYMMDD by Dr. Mu
in thread date span in YYYYMMDD by silent11

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.