Perl

How to create a Perl class? How can you call a method?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

  • Let's create the package: Example.pm
package Example;

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub is_working {
    print "Working!";
}

1;
  • Now we can instance the Example class and call is_working method:
my $e = new Example();
$e->is_working();
# Output: Working!