Recommended Book – Design Patterns

Design patterns : elements of reusable object-oriented software

Although this book was published in 1994, it is still very relevant to software development today.

Having always been self-taught with programming, a read of this has confirmed that I’m either writing code in the right way, or as is quite often the case, shown me some aspects on code design that I should become better in. It’s also a book that I now nearby when I’m coding to ensure I stick to best practices.

From the back cover:

Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.

The authors begin by describing what patterns are and how they can help you design object-oriented software. They then go on to systematically name, explain, evaluate, and catalog recurring designs in object-oriented systems. With Design Patterns as your guide, you will learn how these important patterns fit into the software development process, and how you can leverage them to solve your own design problems most efficiently.

Each pattern describes the circumstances in which it is applicable, when it can be applied in view of other design constraints, and the consequences and trade-offs of using the pattern within a larger design. All patterns are compiled from real systems and are based on real-world examples. Each pattern also includes code that demonstrates how it may be implemented in object-oriented programming languages like C++ or Smalltalk.

It’s also known as the Gang of Four book!

Perl Module of the Day – App::cpanminus

Aka cpanm!

This modules provides a shell command cpanm to install modules directly from CPAN. It takes the pain out of installing dependencies but installing them for you. It really is as simple as it sounds:

$ sudo cpanm Mojolicious
--> Working on Mojolicious
Fetching http://www.cpan.org/authors/id/S/SR/SRI/Mojolicious-4.45.tar.gz ... OK
Configuring Mojolicious-4.45 ... OK
Building and testing Mojolicious-4.45 ... OK
Successfully installed Mojolicious-4.45
1 distribution installed

The above example shows https://metacpan.org/module/Mojolicious being installed to the default system location. If you’re just installing to a local directory do:

$ cpanm --local-lib ~/lib Mojolicious

There are more features, view App::cpanminus on meta::cpan for more information.

Global Search and Replace Regexs

I’ve just been doing some basic text layout clean up code, i.e. getting rid of empty lines, leading/trailing whitespace, etc. You may think a regex like this will get rid of all empty lines:

$text =~ s/ n [ ]* n /n/sxmg;

However, because the second n in the regex matches and is removed strings like

$text = "n n n";

become:

$text = "n n";

Only one line removed since the regex matcher has matched up to the second n in the original string it only sees ” n” and doesn’t remove the second empty line.

You may think about solving it by re-performing the regex a second time, e.g.

$text =~ s/ n [ ]* n /n/sxmg;
$text =~ s/ n [ ]* n /n/sxmg;

Not great as this still wouldn’t capture all empty lines if there were more than two in it, so what about:

while ( $text =~ s/ n [ ]* n /n/sxmg )
{
    1;
}

Yes, that would do it by it isn’t very nice, the best way is to use a “Zero-width positive lookahead assertion”, e.g.

$text =~ s/ n [ ]* (?:n) /n/sxmg;

Nice, but still not quite there, it’ll get rid of all the lines with only white space it it, so we’ll need an extra regex to fully clear up newlines, so the end code will look something like:

$text =~ s/ n [ ]* (?:n) /n/sxmg;
$text =~ s/ nn+ /nn/sxmg;

 

Pretty DBIx::Class output under Catalyst

If you’re using DBIx::Class along with Catalyst and need to view the SQL that DBIx::Class is generating, just set these Environment variables:

DBIC_TRACE=1
DBIC_TRACE_PROFILE=console

The first will turn on the logging, and the second will make it print it out in colour. As you don’t necessarily want this on all the time, you can just enable it on a single line when you start up your catalyst server:

DBIC_TRACE_PROFILE=console DBIC_TRACE=1 ./script/project_server.pl -dr