Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Lately I have been using a particular set of code over and over again, and I finally decided to put it into a module. I would like everyone's overall opinion of this code, and whether or not they think it might even be worthy of CPAN.
package Date::Lastday; $VERSION=0.01; use strict; ## GLOBALS ## my %Max_Days=("Jan"=>"31","Feb"=>"28","Mar"=>"31","Apr"=>"30", "May"=>"31","Jun"=>"30","Jul"=>"31","Aug"=>"31", "Sep"=>"30","Oct"=>"31","Nov"=>"30","Dec"=>"31"); my %Months=("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr", "05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug", "09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec"); ## Functions ## sub new{ my $self=shift; return bless {} $self; } sub last{ my $self=shift; my ($Day,$Month,$Year)=(localtime)[3,4,5]; $Month=sprintf("%02d,($Month+1)); $Year=sprintf("%04d,($Year+1900)); ## Provide for leap years ## my $Leap=$Year % 4; if($Leap==0){ $Max_Days{"Feb"}=29; } if($Day==1 and $Month==1){ # Cover January 1st $Month=12; $Day=31; $Year=$Year-1; }elsif($Day==1){ # Cover the 1st of any month $Month-=1; $Month=sprintf("%02d",$Month); $Day=$Max_Days{$Months{$Month}}; }else{ $Day-=1; $Day=sprintf("%02d",$Day); } return $Month,$Day,$Year; } sub cal{ my $self=shift; my ($Day,$Month,$Year)=(localtime)[3,4,5]; $Month=sprintf("%02d,($Month+1)); $Year=sprintf("%04d,($Year+1900)); ## Provide for leap years ## my $Leap=$Year % 4; if($Leap==0){ $Max_Days{"Feb"}=29; } if($Day==1 and $Month==1){ # Cover January 1st $Month="Dec"; $Day=31; $Year=$Year-1; }elsif($Day==1){ # Cover the 1st of any month $Month-=1; $Month=sprintf("%02d",$Month); $Month=$Months{$Month}; $Day=$Max_Days{$Months}; }else{ $Day-=1; $Day=sprintf("%02d",$Day); $Month=$Months{$Month}; } return $Month,$Day,$Year; } 1; __END__; =pod =head1 NAME Date::Lastday - Perl extension to calculate previous day's date =head1 SYNOPSIS use Date::Lastday; my $Last=new Date::Lastday; my ($Month,$Day,$Year)=$Last->last(); ## OR ## my ($Month,$Day,$Year)=$Last->cal(); =head1 DESCRIPTION Date::Lastday is used to calculate the date of the previous day. It us +es the localtime function of Perl to get the date, then manipulate it + from there. The last function returns the month and day as 2 digit numbers, and th +e year as a 4 digit number. If you wish to have the month returned as + the 3 letter abbreviation (i.e. Jan, Feb, Mar, etc.), use the cal fu +nction. =head2 EXPORT None by default =head1 AUTHOR Thomas Stanley Thomas_J_Stanley@msn.com =head1 COPYRIGHT Copyright (C) 2001 Thomas Stanley. All rights reserved. This program i +s free software; you can redistribute it and/or modify it under the s +ame terms as Perl itself. =head1 SEE ALSO perl(1) =cut

TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out
-- Douglas Adams/Hitchhiker's Guide to the Galaxy

In reply to Module Code Review by TStanley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found