Skip to content

Commit

Permalink
use more methods, fewer hash accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Mar 22, 2009
1 parent 0e87dbe commit 08d9f4c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
6 changes: 2 additions & 4 deletions t/lib/Class.pm
Expand Up @@ -10,9 +10,7 @@ my %STATIC = (
);

my %UNIVERSAL = (
new => sub {
bless { __class__ => $_[0] } => $_[0]->instance_class,
},
new => sub { bless { __class__ => $_[0] } => $_[0]->instance_class, },
name => sub { $_[0]->{name} },
base => sub { $_[0]->{base} },
new_subclass => sub {
Expand All @@ -33,7 +31,7 @@ my %UNIVERSAL = (
my $curr = $self;
while ($curr) {
return 1 if $curr == $super;
$curr = $curr->{base};
$curr = $curr->base;
}
return;
},
Expand Down
11 changes: 4 additions & 7 deletions t/lib/Instance.pm
Expand Up @@ -11,10 +11,7 @@ my %STATIC = (

my %UNIVERSAL = (
class => sub { $_[0]->{__class__} }, # shout out to my homies in python
isa => sub {
my $class = $_[0]->class;
return $class->derives_from($_[1]);
},
isa => sub { return $_[0]->class->derives_from($_[1]); },
);

use metamethod sub {
Expand All @@ -33,16 +30,16 @@ use metamethod sub {

while ($curr) {
# Sadly, this has to be a hash deref until the tests pass once.
my $methods = $curr->{instance_methods};
my $methods = $curr->instance_methods;

$code = $methods->{$method_name}, last
if exists $methods->{$method_name};
$curr = $curr->{base};
$curr = $curr->base;
}

unless ($code ||= $UNIVERSAL{$method_name}) {
my $msg = sprintf "no instance method %s on %s(%s)",
$method_name, ref($invocant), $invocant->{__class__}{name};
$method_name, ref($invocant), $invocant->{__class__}->name;
die $msg;
}

Expand Down

0 comments on commit 08d9f4c

Please sign in to comment.