

sub memoize_method {
  my ($methname, $slotname) = @_;
  die unless defined $slotname;
  my $caller = caller;
  $methname = "$caller\::$methname" unless $methname =~ /::/;
  my $method = \&{$methname};
  my $stub = sub {
    my ($self, @args) = @_;
    my $key = join ",", @args;
    return $cache->{$key} if exists $self->{slotname}{$key};
    $self->{$slotname}{$key} = $method->(@args);
  };
  *$methname = $stub unless defined wantarray;
  $stub;
}

