in reply to Re: Extra-lazy object oriented programming
in thread Extra-lazy object oriented programming
Maybe it's better to add the functions to Moo.pm. So I copied Moo.pm to Moox.pm and put it in the lib directory. Then changed "package Moo;" to "package Moox;". And then added a few functions after 'has': (with apologies to Matt S. Trout)
_install_tracked $target => hasrw => sub { my @words = @_; if (@_ == 1 and $words[0] =~ m/ /) { # they passed a string with space separated words @words = split ' ', $words[0]; } push @words, 'is', 'rw'; # not sure if this is the right way to call has $MAKERS{Testpool}{exports}{has}->(@words); return; }; _install_tracked $target => hasro => sub { my @words = @_; if (@_ == 1 and $words[0] =~ m/ /) { @words = split ' ', $words[0]; } push @words, 'is', 'ro'; $MAKERS{Testpool}{exports}{has}->(@words); return; }; _install_tracked $target => hasrwd => sub { my ($name, $default) = @_; my @words = ($name, 'default', $default, 'is', 'rw'); $MAKERS{Testpool}{exports}{has}->(@words); return; }; _install_tracked $target => hasrod => sub { my ($name, $default) = @_; my @words = ($name, 'default', $default, 'is', 'ro'); $MAKERS{Testpool}{exports}{has}->(@words); return; };
Then other code can use it like this:
package Testpool { use Moox; # r/w attr defined by string hasrw 'name default samantha'; # ro attr defined by string hasro 'height default 1777'; # ro attr defined by list hasro qw/weight default 99/; # r/w attr with default value hasrwd 'cheezburgers', 22; # ro attr with default value hasrod 'cats', 1; };
|
|---|