September 2015 Archives

Improved DBIx::Class usage with arbitrary SQL

A few months ago I write about using arbitrary SQL to generate DBIx::Class resultsets. These DBIx::Class::Report resultsets are read-only, but I found that I needed to add additional methods to each result and now I can. This makes the software much more flexible.

Fluent interfaces in Perl 6

I've been digging into Perl 6 more lately and I noticed the Wikipedia example of fluent interfaces didn't have a Perl 6 example, so I fixed that.

To be fair, Martin Fowler's explanation (as usual) of fluent interfaces does a much better job of explaining them, but a key point is that setters have a return value. For many fluent interfaces, the setters set a value and actually return a new instance of a different object for you to call methods on. Thus, the examples in Wikipedia don't always meet the criteria of a fluent interface, but I added a Perl 6 version that closely modeled the PHP version (but more concisely, and with much better type safety). I sidestepped the entire fluent interface debate.

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.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/