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;


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{$_}++,
            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



Ok, I took up JT's challenge to build something. Here is the MVP launch of it only works on https right now so don't try 80. Mojolicious, DBIx Class (Candy) and a healthy dose of DateTime::Set and a couple of other DT modules with Bootstrap frontend. This app is meant to be a tool for the growing group of tech savvy, [home|un|hack] schooling crowd, with features for those that are location independent. I already have a roadmap of about 30 items to add and to start work on the mobile syncable version. All feedback is welcome. Thanks.