views:

469

answers:

7

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.

Cheers

+9  A: 

A class is a type (like "SUV"). An object is an instance of a class ("David's SUV").

cletus
I've always preferred the bawdier form: You can't sleep with Woman (class), but you can sleep with Debbie (object). Of course, modify to suit your personal tastes....
nezroy
But you can hear Woman::roar(). This is a static method. :-)
Bill Karwin
+2  A: 

Objects are single instances of a Class.

Adam Peck
+3  A: 

Another way to think of it is a class is a blueprint for how an object will be built.

Spencer Ruport
+1  A: 

You are an object of class Human

(Classes in Perl are modules with some special qualities, you should better first understand only the general case).

Null303
+5  A: 

Perl-wise:

  • A class is a package--a specification. A set of behaviors and data mainly to aid those behaviors.
  • An object is typically a "hashref", that is a collection of specific data allowed by the behavior specification in the package (and inherited behaviors).

Now, a hashref might hold a code reference. In most cases, that's behavior. But the only way the object could use that specific behavior is for that to be specified by some class behavior inherited (or mixed in) that expects that there might be a coderef sitting at that location and invoke it.

Axeman
A: 

I don't see people using the terms the same way in other languages. That may be one reason for the question. I think maybe PHP users say "class" when they should say "object", a lot of the time?

Anyway, what about this example -- imagine you had to create two different database connections for two different databases:

my $oracle_database_handle = DBI->connect( <oracle connection details here> );

my $mysql_database_handle =  DBI->connect( <mysql connection details here>  );

you would have created two objects for doing two different things, but they're both the same kind of thing -- DBI database connections.

AmbroseChapel
+13  A: 

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";

    return;
}

# Hackers can say which languages they like best.

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

    my $languages = $self->languages;

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

    return;
}

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:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

use Hacker;    # Assuming the above is in Hacker.pm

# $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.

$pjf->say_hello;
$pjf->praise_languages;

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

# So can $jarich

$jarich->say_hello;
$jarich->praise_languages;

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,

Paul

pjf
lexu
See now I thought that was overly wordy but hey thats just me. Good answer though. :)
cletus