G'day.

In implementing a Moose::Role I have run into a question that I can't find an
easy or obvious answer to in the documentation.

So, in my role I have several methods that need to perform some non-trivial
operations that are identical, but which are also strictly internal: they are
not part of the API, because they are not really useful outside the code,
especially if I change the implementation at all.[1]

So, at the moment I do this:

  package Example;
  my $find_stage = sub { my ($self, $arg) = @_; ...; };

  sub dance {
     my ($self, $where) = @_;
     my $stage = $find_stage->($self, $where);
     ...;
  }

  sub sing {
     my ($self, $where) = @_;
     my $stage = $find_stage->($self, $where);
     ...;
  }

That ensures that I don't go leaking the 'find_stage' method as part of the
API, or conflicting with other roles or the base class, or whatever.  The
"Stage", for what it is worth, is an implementation detail; it isn't useful
without the context of the role.


So, is there a better way to implement a private method — one that isn't
available outside the context of the role, and isn't exported?


Hmmm.  In writing this all down I had to think about it, and it occurs to me
that I might be facing this problem because of a higher level design decision.

At the moment I create one attribute each time a "stage" is created on the
class; the methods in the role then look those up using the MOP by name in
order to obtain the internal implementation.

I could, I guess, replace that with a single attribute containing a hash,
mapping names to "stage" instances.  The sing and dance methods would then
need to do a hash lookup in a know attribute, so the entire helper could be
eliminated...


In this case the "stage" is a semantic feature similar enough to an attribute,
so an object could conceivably have quite a few of them, but the average would
probably be somewhere around half a dozen or so.


Anyway, I would welcome your advice on the topic.

Regards,
        Daniel

Footnotes: 
[1]  ...and I have plans that imply this API will probably need to change at
     some point, too.

-- 
✣ Daniel Pittman            ✉ [email protected]            ☎ +61 401 155 707
               ♽ made with 100 percent post-consumer electrons

Reply via email to