in reply to Moose: Adding methods to a class via attribute traits

Let's assume that the way Moose::Meta::Attribute::Native does it is the best possible way (given that it's part of Moose core now). Then the best possible way is, rather than including a BUILD method, use:

after 'install_accessors' => sub { my $self = shift; # ... };

Replies are listed 'Best First'.
Re^2: Moose: Adding methods to a class via attribute traits
by Nasrudin (Acolyte) on Jan 17, 2012 at 21:25 UTC

    The idea to look there didn't occur to me, so I looked in that source. Sadly I did not find any reference to using a method modifier on 'install_accessors' (which is really a method in Moose::Meta::Atribute).

    When I blindly replace my BUILD definition with your method modifier idea, I get:

    The method 'install_accessors' was not found in the inheritance hierar +chy for Me at /usr/local/lib/perl5/site_perl/5.10.1/mach/Class/MOP/Cl +ass.pm line 659 Class::MOP::Class::__ANON__('Moose::Meta::Class=HASH(0x8025684c8)' +, 'install_accessors') called at /usr/local/lib/perl5/site_perl/5.10. +1/mach/Class/MOP/Class.pm line 694 Class::MOP::Class::add_after_method_modifier('Moose::Meta::Class=H +ASH(0x8025684c8)', 'install_accessors', 'CODE(0x802568d98)') called a +t /usr/local/lib/perl5/site_perl/5.10.1/mach/Moose/Util.pm line 229 Moose::Util::add_method_modifier('Moose::Meta::Class=HASH(0x802568 +4c8)', 'after', 'ARRAY(0x802443288)') called at /usr/local/lib/perl5/ +site_perl/5.10.1/mach/Moose.pm line 77 Moose::after(undef, 'install_accessors') called at /usr/local/lib/ +perl5/site_perl/5.10.1/mach/Moose/Exporter.pm line 293 Moose::after('install_accessors', 'CODE(0x802568d98)') called at . +/testm line 41

    This makes some sense because my class uses Moose, doesnt use an 'extends' keyword, and isn't necessarily a subclass of Moose::Meta::Attribute. Obviously I'm missing something here, but what?

      Replying to myself here because I was able to use your clue to find out not only what you appear to have meant but a solution. I'll update the main text above in a moment.