How can I overload methods in Moops?

I would like to overload some methods in Moops.

I tried the following code:

method setIdNum() {
      print "Please enter ID number: ";
      chomp (my $input = <STDIN>);
      $self->$idNum($input);
}

method setIdNum(Int $num) {
      $self->$idNum($num);
}

      

But these are errors saying setIdNum is overridden.

+3


source to share


1 answer


If you want multimethods, you must ask them explicitly by prefixing multi

the keyword method

:

multi method setIdNum() {
  print "Please enter ID number: ";
  chomp (my $input = <STDIN>);
  $self->$idNum($input);
}

multi method setIdNum(Int $num) {
  $self->$idNum($num);
}

      



You may also need to explicitly request Kavorka support inside your class declaration:

class Whatever {
    use Kavorka qw( multi method );
  ...

      

+3


source







All Articles