Subject Verb Object notation; declarative Perl without the framework

If you’ve read Curtis “Ovid” Poe’s articles on the declarative framework for Tau Station Link and Link, you are undoubtedly aware of many of the benefits this style of programming can bring. It decouples the “what” from the “how”, encourages discrete functions and prevents the OO trap of “god objects”. The result is software that is easy to test, robust and very flexible. Inserting steps, reording steps etc… are done much…

Roles, h'uh, what are they good for?

What is a role? Put simply, roles are a form of code reuse. Often, the term shared behavior is used. Roles are said to be consumed and the methods ( including attribute accessors ) are flattened into the consuming class.

One of the major benefits of roles is they attempt to solve the diamond problem encountered in multi-inheritance by requiring developers to resolve name collisions manually that arise in multi-inheritance. Don't be fooled however, roles are a form of multi-inheritance.

I often see roles being used in ways they shouldn’t be. Let’s look at the mis-u…

MongoDB REST Interface

Looking at the list of REST Interfaces listed on Mongo's site, there isn't a Perl solution. Well this just won't stand. So after looking at the python program to see if I missed something about what this would entail, I hacked out this gist.

There are some limitations - you have to let mongo generate your _id - and the PUT /:db/:collection/:id route doesn't use the id portion from the route; it's this way to work with the JS framework generated URLs I am working with, and some of the code could be abstracted more. Howeve…

Announcing Test::mongod

After having done a couple of projects with MongoDB and working on and off on a personal project, I found myself wanting something like Test::mysqld, but alas it did not exist on CPAN. So here it is Test::mongod. It's rough but will get you a throw away instance of MongoDB in a /tmp directory that will be cleaned up when the script ends. The code can also be found on GitHub.

use Test::mongod;

then....

my $mongod = Test::mongod->new;  # get a temp db on a random port.
my $port = $mongod->port; # get the port to feed to your client app.

Be aware that depending on your hardware, this will block till the server is listening and ready to receive queries - which could be instant or take several seconds. To monitor startup get the db path and then tail the log file in another terminal.

diag $mongo->dbpath

Now to make some sort of fixture loader. Hmmm... what should it be call?

Tie::File don't while(<>)

Consider the common case where you want to take a text file and walk it doing various operations such as stripping whitespace, looking for duplicates, sorting or other common operations. I was recently asked to work on a program where part of the process is 'cleaning' uploaded files before doing other things with it. This process was over 40 lines of subs opening old and new filehandles, doing the standard while(<>) {} and then renaming new file to old file. If you find yourself wanting to do something you would normally do on an array, but on a file and often in place, consider Tie::File before while()ing away at it. The documentation has many examples and there are several examples on the web including at Perl Monks. In this case I was able to whittle this down to this:

tie my @file, 'Tie::File', $file;
@file = grep !$seen{$_}++,
            sort,
            map s/$RE{ws}{crop}//g,
            map lc, @file;

untie @file;

If you're working with CSV or other delimited files, consider Tie::Array::CSV

Cheers