DavidSzczesniak
12/20/2017 - 2:50 PM

Methods

This is a function assiociated with a class. The same way that a function belongs to a namespace, a method belongs to a class.

# When you call a method, you do so with an invocant. 
# When you call a method on an object, that object is the invocant:
my $choco = Cat->new;
$choco->sleep_on_keyboard;

# A method's first argument is its invocant($self). Suppose a Cat can meow():
package Cat {
  use Moose;
  
  sub meow {
    my $self = shift;
    say 'Meow!';
  }
}
# the cat always meows three times at 6 am 
my $fuzzy_alarm = Cat->new;
$fuzzy_alarm->meow for 1..3;