in reply to Re: Extra-lazy object oriented programming
in thread Extra-lazy object oriented programming

Thanks Toby. The lists of names is nice, but then they don't have defaults. Which would mean the constructors would have to hold the default values. I'd rather the defaults were in the definition.

Moose::Tiny and Object::Tiny only do read-only, so they won't work.

Something like this might work:

package Haz; use Modern::Perl; use lib 'lib'; use Moo; use Acme::Mo; # has split / /, 'cheezburgers is rw default 22'; haz 'cheezburgers is rw default 22'; rw 'weight default 100'; ro 'name default barbara'; rwdef100 'money'; package main; run(); sub run { my $haz = Haz->new; say "haz = ", Dumper($haz); say 'i haz ', $haz->cheezburgers, ' cheezburgers'; $haz->weight(99); say 'weight: ', $haz->weight; say 'name: ', $haz->name; #$haz->name('jane'); say "it all worked"; } __END__
# lib/Acme/Mo.pm package Acme::Mo; use Modern::Perl; use Filter::Simple; sub hazmat { my $def = shift; $def =~ s/['"]//g; $def =~ s/(\w+)/'$1',/g; my $line = "has $def;"; return $line; } sub hasrw { my $def = shift; $def .= ' is rw'; return hazmat($def); } sub hasro { my $def = shift; $def .= ' is ro'; return hazmat($def); } sub hasrwdef100 { my $def = shift; $def .= ' is rw default 100'; return hazmat($def); } FILTER { s/^ro (.*);/hasro($1)/gem; }; FILTER { s/^rw (.*);/hasrw($1)/gem; }; FILTER { s/^haz (.*);/hazmat($1)/gem; }; FILTER { s/^rwdef100 (.*);/hasrwdef100($1)/gem; }; 1; __END__

Replies are listed 'Best First'.
Re^3: Extra-lazy object oriented programming
by tobyink (Canon) on Sep 09, 2014 at 06:50 UTC

    If want defaults:

    use Moo; has [qw/ foo bar baz /], is => "lazy", builder => 1; sub _build_foo { [] } sub _build_bar { {} } sub _build_baz { 42 }