I would like ask the group for advice on module naming, as I have seen some missteps in the past.

I have written a small module which takes the output of DBI::fetchall_arrayref() or its ilk, and generates Moose objects from it.

Showing great imagination, I have called the module DBIx::BuildMooseObjects. I picked "DBIx::" as the module's functionality seems to fit the DBIx namespace.

There are two main exported routines, rather clumsily named mk_AoMobj_from_2d_array() and mk_complex_Mobj_from_2d_array(). The first returns a (ref to) an array of (already-declared) Moose objects, the second (a ref to) an array of complex, nested Moose objects (sub-objects declared as "isa => 'ArrayRef[...]' " )

As an example, mk_complex_Mobj_from_2d_array() is used as (the text is from the POD):

In database SQL queries involving joins, where there is a one-to-many
relationship between the tables in the query, we often see "nested" data
output of this kind:

<---  Patient   ---> <---      Visit       ---> <--   Test   -->
  Name    | Sex | Age | MD Name     | Date       |  Name  | Result
  --------+-----+-----+-------------+------------+--------+-------
  J Smith | M   | 35  | A Jones, MD | 12/08/2009 | B.P    | 120/80
  J Smith | M   | 35  | A Jones, MD | 12/08/2009 | Weight | 156
  J Smith | M   | 35  | A Jones, MD | 03/19/2010 | B.P    | 122/88
  B White | F   | 53  | M Wright MD | 07/06/2005 | Chol   | 190
  B White | F   | 53  | M Wright MD | 07/06/2005 | ESR    | 21
  B White | F   | 53  | F Albert MD | 08/16/2011 | B.P.   | 125/82

In this example a patient makes multiple visits to an MD, undergoing
multiple test per visit.

  package Test;
  use Moose
  has qw(name, result) => (is => 'rw', isa => 'Str');

  package Visit;
  use Moose;
  has qw(date md_name) => (is => 'rw', isa => 'Str'            );
  has Tests            => (is => 'rw', isa => 'ArrayRef[Test]' );

  package Patient;
  use Moose;
  has qw(name sex age) => (is => 'rw', isa => 'Str'             );
  has Visits           => (is => 'rw', isa => 'ArrayRef[Visit]' );
  no Moose;

  package main;

  use DBI;
  use DBIx::BuildMooseObjects;

  my $dbh = DBI->connect(...);

  my $ary_ref = $dbh->selectall_arrayref(
      "SELECT p.name, p.age, v.mdname, v.date, t.name, t.result
       FROM   Patient p, Visit v, Test t
       WHERE  p.pat_key   = v.pat_key
         AND  v.visit_key = t.visit_key" );

  my $patient_arry_ref = mk_complex_Mobj_from_2d_array (
                          data => $ary_ref,
                          desc => [
                            class  => 'Patient',
                            name   => 0,
                            age    => 1,
                            Visits => [
                              class   => 'Visit',
                              md_name => 2,
                              date    => 3,
                              Tests   => [
                                class  => 'Test',
                                name   => 4,
                                result => 5,
                              ] # tests
                            ] # visits
                          ] # patients
                                                      );

The integers after the attribute names (C<<md_name => 2>>, etc) are the
zero-indexed column numbers of the attribute in the SQL output.


So:
(1) Does DBIx::BuildMooseObjects seem a reasonable name, and
(2) Do others feel the same as I do, that the exported method names could be less ugly (but in my case, can't think of better ones [insert metaphor involving woods & trees]).

All comments and suggestions welcome.

Thanks,

Sam Brain

--
Sam Brain
Department of Radiation Oncology
Stanford Medical Center
Stanford, CA 94305

Reply via email to