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.

10 thoughts on “Class::Accessor can has “has”

  1. brunov

    Cool! Does it also support the following?

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

    Because that would be cool too.

  2. Adam Kennedy

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

  3. Shawn M Moore

    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. Marty Post author

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

  5. Pingback: 開発日記 » Class::AccessorがMooseスタイルに対応したらしい

  6. Pingback: 開発日記 » アクセサを作るモジュールをゴニョゴニョ。

Leave a Reply

Your email address will not be published.