[Catalyst-dev] Moose + Catalyst port

Jonathan Rockway jon at jrock.us
Sun Mar 16 02:39:14 GMT 2008


* On Sat, Mar 15 2008, Dave Rolsky wrote:
> On Sat, 15 Mar 2008, Jonathan Rockway wrote:
>
>> This doesn't really clutter the namespace because it's just modifying
>> things.  I suppose plugins can add methods, but let's not document that
>> and see what happens.
>
> What I'd really like is a way to add _one_ method via a plugin, while
> keeping most of the logic outside the plugged-into class.
>
> For example, I like to add a "$c->user()" (or it could be
> "$c->request()->user()") method. I don't need to add the
> "_determine_user_from_cookie()" method I'm using under the hood,
> though.

I've gotten pretty religious about not cluttering up my namespace with
"internal" methods that I never expect to override (or modify) in
subclasses.  To achieve this, there are a few things I do.

First, lexically-scoped subs:

  my $internal_utility = sub { ... };

  sub external_interface {
     my ($self) = @_;
     $internal_utility->($self, ...);
  }

The disadvantage here is that subclasses can't do anything with these,
and testing the internal methods by calling them directly is not
possible.  That can be worked around, perhaps with PadWalker.

Another helpful policy is to never name things that you're only going to
use once.  Instead of:

  has 'foo' => (
    is      => whatever,
    isa     => foobar,
    default => \&_make_foo,
  );

  sub _make_foo { }
    
I prefer:

  has 'foo' => (
    is      => whatever,
    isa     => foobar,
    default => sub { ... },
  );

I can't think of a good mechanism for only applying public methods.  The
best thing to do is relax and not think about it too much.  Some
namespace clutter is inevitable, and the nice part of roles is that
Moose will refuse to apply roles that contain conflicting methods.

As an example of "relaxing", when I was learning CLOS I got really
stressed out about (set-slot).  That's going to totally break my class
if someone calls (set-slot 'foo bar) instead of going through my
accessor.  What a terrible object system!111!!  *fume, fume*

Then I realized that you just don't call set-slot.  It's there, but you
don't have to use it.  Private methods are the same way.  Don't touch
them and they won't bite you :)

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"



More information about the Catalyst-dev mailing list