Dave Cross and Modern PERL
At this year's YAPC::EU, we've been having a blast in Granada, Spain, an incredibly beautiful city. The conference has been fun and Dave Cross gave a great lightning talk about Modern PERL (sic). These are devs who are using 5.8, often aren't allowed to use modules, and use CGI.pm for param handling, but print cookies manually. In a similar spirit, I present a subroutine from some client code. It's here with their permission, and it's one of reasons they've hired All Around The World to fix their system. Pay close attention to the sprintf
lines.
Enjoy!
sub getDate {
my $DATE = "0";
# Get the date
($day, $month, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
# Reformat numbers to have two digits
$day = sprintf ( "%.2d", $day % 100 );
$month++;
$month = sprintf ( "%.2d", ($month++) % 100 );
$hour = sprintf ( "%.2d", $hour % 100 );
$min = sprintf ( "%.2d", $min % 100 );
$sec = sprintf ( "%.2d", $sec % 100 );
# Fix the year
$year = $year + 1900;
# Format the date.
$DATE = "$day$month$year$hour$min$sec";
# Return the date
return $DATE;
}
I should add that this is some of the nicer code in their system.
I believe it is worth mentioning that the whole function can be replaced by
Why not use strftime() from POSIX?
BTW, SI standard is year, month, day, hour, minute, second.
I hope there's a video of Dave's talk coming because it sounds like he's seen where I work... Sigh
Though, to be fair, we're using 5.8.8. ;)
shawncorey: we're not going to use that because that's a part of the system we're no longer using, nor do we need its functionality. I promise you we didn't write that code :)
This is ... creative. In our code, I usually just find very, very boring if ( $hour < 9 ) stuff. But modulo 100? Too good to call it PERL.
@Ovid: POSIX is a standard module. It comes installed with Perl. For a list of all the standards modules, see
perldoc perlmodlib
http://perldoc.perl.org/perlmodlib.html.
@shawnhcorey: I'm pretty sure that Ovid knows that POSIX is a standard module :-)
Maybe the author's productivity was judged by LOC/D (Lines Of Code written / Day)?
a nice example. one can see the stumbling around the coder experienced reading perl documentation, trying stuff haphazardly until getting the desired result, never bothering to understand or clean up the failed attempts.