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 comments ↓

#1 brunov on 09.21.09 at 02:45

Cool! Does it also support the following?

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

Because that would be cool too.

#2 Yuval Kogman on 09.21.09 at 03:03

FWIW, If you want to go the other way (run on Moose but use C::A syntax instead of the other way around), the MooseX::Emulate::Class::Accessor::Fast module provides a compatible API (it was developed so that old Catalyst code could run on Moose unmodified).

#3 Adam Kennedy on 09.21.09 at 09:53

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

#4 Shawn M Moore on 09.21.09 at 15:39

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! :)

#5 Marty on 09.21.09 at 23:25

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

#6 Marty on 09.21.09 at 23:26

Calling has in a loop should work.

#7 開発日記 » Class::AccessorがMooseスタイルに対応したらしい on 09.22.09 at 14:54

[...] Class::Accessor can has “has” — バカな火星人 YAPCでdisられたから対応したとかなんとか Class::Accessor のインターフェイスがあんまり好きじゃなかったんだけども、素敵になったので様子見て使おうと思う。 今は、モジュール使わずにそのまま書いてるんです>< [...]

#8 mxey on 09.22.09 at 16:14

Actually I like the classic Class::Accessor API better ;)

#9 Marty on 09.22.09 at 22:48

It hasn’t gone away.

#10 開発日記 » アクセサを作るモジュールをゴニョゴニョ。 on 11.13.09 at 10:21

[...] こんな感じでMooseの書き方も出来るけど、is指定が好きじゃない。正味、読み書き両方出来るのだけで良い。 [...]

Leave a Comment