Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I suggest looking into the Time::Piece module, to get rid of all those nondescriptive subscripts.

    --
    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • Something like this? :)
      use Time::Piece;
      use Time::Seconds;

      my $time  = localtime();
      my $end   = $time - ONE_DAY * $time->wday;
      my $start = $end - ONE_DAY * 6;

      my ($startdate, $enddate) = map $_->strftime('%Y%m%d'), $start, $end;

      print "Start: $startdate, End: $enddate\n";
  • It's weird how many newbies fall back to this kind of code:

      if (length($month) < 2) {$month = "0$month"}

    Horrors. My advice: use sprintf(). In fact, you can combine the sprintf for year, month and day into

     $enddate = sprintf "%04d%02d%02d", $y+1900, $m+1, $d;

    While we're at it: there's still more you can group, like this getting the year, month and day pout of a localtime() list value at once, ready for the above snippet:

      my($d, $m, $y) = (localtime($endseconds))[3, 4, 5]

    • Matt's Script Archive code did it that way, so a lot of junior perlhackerwannabe's also adopted that style.

      {sigh} Bad memes.

      --
      • Randal L. Schwartz
      • Stonehenge
      • What does it say about me if I didn't adopt that style courtesy of Matt's Script Archive?

        What can I say? Being new to the language, I didn't remember sprintf when writing the code, opting instead for something easier, simpler, and possibly more legible. 8)

  • Thanks for your suggestions, I appreciate the feedback!

    Argh, this is frustrating. How is a novice Perlie to find out about this sort of thing? I search CPAN aggresively for "calendar week", "previous week", and something else I can't recall right now. I thumbed through Ye Olde Cookbook, the Llama, and The Camel. What else should I have done?

    How did you folks find out about Time::Piece initially?

    John

    • I can't remember how I first found out about Time::Piece. I know that I had a design on my whiteboard for an identical module for the first six months of 2000, and then Matt Sergeant suddenly wrote it. No, he didn't have any contact with my whiteboard, but he did have an identical design from Larry Wall in the early part of that year.

      I suggest looking through new modules [cpan.org] each day; it will give you a chance to become familiar with what's out there. You could go read the module list, but it would take fo

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers