Class::Accessor can has “has”

I maintain the Class::Accessor module. It appears to be used a lot, but the API is a bit ugly. In YAPC::Asia the ugly API was criticised in at least three different talks, and each time it was compared to the fashionable Moose API.

In one of these talks JRockway asked Shawn Moore how to turn a bad API into a good API, so I’m going to try that: adding antlers to Class::Accessor!

So now instead of writing:

package Foo;
use base qw(Class::Accessor);
Foo->mk_accessors(qw(alpha beta gamma));

If you prefer Moose-style you can write:

package Foo;
use Class::Accessor "antlers";
has alpha => ( is => "rw" );
has beta  => ( is => "rw" );
has gamma => ( is => "rw" );

The original API is still available, and everything is the same underneath.


  1. Cool! Does it also support the following?

    has $_ => ( is => ‘ro’ ) for qw(foo bar baz);

    Because that would be cool too.

  2. Unless you know the in-jokes, “antlers” makes no sense at all. Recommend just using something more like ‘Moose’ or whatever.

  3. Oh damn. I would have been more diplomatic if I had known you were the maintainer of Class::Accessor. But then again, it worked, so.. cheers! :)

  4. You can also say use Class::Accessor 'Moose-like'; if you want to.

Comments are closed.