singingfish singingfish - 1 year ago 43
Perl Question

What's the difference between an object and a class in Perl?

I'm having a little trouble getting my head around the conceptual difference between an object and a class. I don't really understand the distinction between the two in any programming language, but currently I'm working with Perl, and Moose, so I'd prefer an explanation using those things.


pjf pjf
Answer Source

There are lots of "a class is a blueprint, an object is something built from that blueprint", but since you've asked for a specific example using Moose and Perl, I thought I'd provide one.

In this following example, we're going have a class named 'Hacker'. The class (like a blueprint) describes what hackers are (their attributes) and what they can do (their methods):

package Hacker;       # Perl 5 spells 'class' as 'package'

use Moose;            # Also enables strict and warnings;

# Attributes in Moose are declared with 'has'.  So a hacker
# 'has' a given_name, a surname, a login name (which they can't change)
# and a list of languages they know.

has 'given_name'       => (is => 'rw', isa => 'Str');
has 'surname'          => (is => 'rw', isa => 'Str');
has 'login'            => (is => 'ro', isa => 'Str');
has 'languages'        => (is => 'rw', isa => 'ArrayRef[Str]');

# Methods are what a hacker can *do*, and are declared in basic Moose
# with subroutine declarations.

# As a simple method, hackers can return their full name when asked.

sub full_name {
    my ($self) = @_;   # $self is my specific hacker.

    # Attributes in Moose are automatically given 'accessor' methods, so
    # it's easy to query what they are for a specific ($self) hacker.

    return join(" ", $self->given_name, $self->surname);

# Hackers can also say hello.

sub say_hello {
    my ($self) = @_;

    print "Hello, my name is ", $self->full_name, "\n";


# Hackers can say which languages they like best.

sub praise_languages {
    my ($self) = @_;

    my $languages = $self->languages;

    print "I enjoy programming in: @$languages\n";


1;   # Perl likes files to end in a true value for historical reasons.

Now that we've got our Hacker class, we can start making Hacker objects:

use strict;
use warnings;
use autodie;

use Hacker;    # Assuming the above is in

# $pjf is a Hacker object

my $pjf = Hacker->new(
    given_name => "Paul",
    surname    => "Fenwick",
    login      => "pjf",
    languages  => [ qw( Perl C JavaScript) ],

# So is $jarich

my $jarich = Hacker->new(
    given_name => "Jacinta",
    surname    => "Richardson",
    login      => "jarich",
    languages  => [ qw( Perl C Haskell ) ],

# $pjf can introduce themselves.


print "\n----\n\n";

# So can $jarich


This results in the following output:

Hello, my name is Paul Fenwick
I enjoy programming in: Perl C JavaScript


Hello, my name is Jacinta Richardson
I enjoy programming in: Perl C Haskell

If I want I can have as many Hacker objects as I like, but there's still only one Hacker class that describes how all of these work.

All the best,