-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c47bc8a
commit c30cf5c
Showing
5 changed files
with
77 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package Mo; require strict; require warnings; $Mo::VERSION = '0.16'; | ||
package Mo; require strict; require warnings; $Mo::VERSION = '0.17'; | ||
sub import {strict->import;warnings->import;my $p=caller;@{$p.'::ISA'}=$_[0]; | ||
*{$p.'::extends'} = sub {@{(caller).'::ISA'}=@_;eval "require $_" for @_}; | ||
*{$p.'::has'} = sub { my ($n, %a) = @_; my($d,$b)=@a{qw(default builder)}; | ||
*{(caller)."::$n"} = $d ? sub { $#_ ? ($_[0]{$n} = $_[1]) : (exists $_[0]{$n}) | ||
? $_[0]{$n} : ($_[0]{$n} = $d->($_[0])) } : | ||
$b ? sub { $#_ ? ($_[0]{$n} = $_[1]) : (exists $_[0]{$n}) | ||
? $_[0]{$n} : ($_[0]{$n} = $_[0]->$b) } | ||
: sub { $#_ ? $_[0]{$n} = $_[1] : $_[0]{$n} } }; | ||
} sub new {my $s=bless {@_[1..$#_]},$_[0]; $s->can('BUILD')&&$s->BUILD;$s} | ||
: sub { $#_ ? $_[0]{$n} = $_[1] : $_[0]{$n} } }; } | ||
sub _ { my ($a, $c) = @_; for my $p (@{$c."::ISA"}) { next if $p eq 'Mo'; | ||
unshift @$a, $p; _($a, $p); } @$a; } | ||
sub new { my $c=shift; my$s=bless{@_},$c; | ||
do{&{$_."::BUILD"}($s) if defined&{$_."::BUILD"}} for (_([$c],$c)); $s } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use Test::More; | ||
|
||
plan tests => 3; | ||
|
||
$main::count = 1; | ||
|
||
package Foo; | ||
use Mo; | ||
has 'foo' => (is => 'rw'); | ||
sub BUILD { | ||
my $self = shift; | ||
$self->foo($main::count++); | ||
} | ||
|
||
package Boo; | ||
use Mo; | ||
has 'boo' => (is => 'rw'); | ||
sub BUILD { | ||
my $self = shift; | ||
$self->boo($main::count++); | ||
} | ||
|
||
package Bar; | ||
use Mo; | ||
extends 'Foo', 'Boo'; | ||
has 'bar' => (is => 'rw'); | ||
|
||
package Baz; | ||
use Mo; | ||
extends 'Bar'; | ||
has 'baz' => (is => 'rw'); | ||
sub BUILD { | ||
my $self = shift; | ||
$self->baz($main::count++); | ||
} | ||
|
||
package Gorch; | ||
use Mo; | ||
extends 'Baz'; | ||
has 'gorch' => (is => 'rw'); | ||
|
||
package main; | ||
|
||
my $g = Gorch->new; | ||
is $g->boo, 1, 'boo builds first'; | ||
is $g->foo, 2, 'foo builds second'; | ||
is $g->baz, 3, 'baz builds third'; |