Apologies to those that have OO steeped in their blood. I am a wary traveller in OO space, maybe I am an technician, not an architect at heart. So for me, no sweeping frameworks unless and until they are needed. And, frankly, one can go a long way on procedural code with subroutines to gather repetitive code sequences.
(And don’t get me started on functional programming…)
Some time ago, I was tasked to write a LIMS in old perl. Physical ‘aliquots’ with injections of various liquids would be combined and recombined by bio-machines. This led to a dawning realization that these aliquot objects could be modelled in an object style with parent / child relationships. After a few weeks, I proudly delivered my lowly attempt at ‘Mu’ for this (and only this) problem. Kudos to the P6 team – after a couple of weeks in here, it’s just sensational the level of OO power that the real Mu delivers:
Now, hard at work, on the perl6 version of Physics::Unit, I am wondering how to put the OO theory into productive practice. One of my aims was to find a medium sized (not core tools) problem that (i) I know something about and (ii) would be a good-sized problem to wrangle.
So I am enjoying the chance to design some classes and some interfaces that will make this all hang together. But – as an explorer, it has become clear that I only have three options. The problem runs like this:
- There is a parent Measure class that contains the methods for any real-world measurement handling dimension, units, value and errors.
- Then there are child classes for my Distance $d = ’42 m’, my Time $t = 3 s’, etc.
- First level, I have an operation like … my $r = ’23 ft’ + $d;
Initially I had some success with object types ::T – but these only let you read the type and duplicate if needed for a new left hand side container. Then I tried the built in (shallow) clone method. But…
- What about the operation … my $s = $d / $t?
- How do I create a new Speed object programatically?
Ultimately, thanks to rosettacode.org, I worked out that $x.perl.EVAL with some ~~ s/// substitions on the way would do the trick!
Phew. Please comment below if you have a better way to share – or would like to point out the risks of this technique…