Class 12 - Object Oriented Programming

Finding Modules

A simple module (hello.pm) and two programs (main.pl and main2.pl) that try to use it. Assuming all three are in the same directory, main.pl works provided you either set PERL5LIB to that directory, or you run the program from that same directory. main2.pl automatically adds the directory to the library search path using FindBin.

hello.pm

sub hi { print "Hello, world\n"; }
1;

main.pl

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

hi();

main2.pl

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

use FindBin;
use lib $FindBin::Bin;
use hello;

hi();

Multiple packages in one file

two-in-one.pl

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

B::hi();
A::hi();
A::bye();

package A;
sub hi { print "Hello from A.\n"; }
sub bye { print "Goodbye.\n"; }

package B;
sub hi { print "B says hi!\n"; }

A Simple "Hello" module

This simple module says hello to people. Unfortunately it doesn't keep track of who it has said hello to. We could use a global hash to track this, but our goal is to learn about objects.

hello.pm

package hello;
my $counter = 0;
sub hi {
	my($name) = @_;
	$counter++;
	print "Hello, $name (I've said hello to $name $counter times)\n";
}
1;

main.pl

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

use FindBin;
use lib $FindBin::Bin;
use hello;

hello::hi("Alan");
hello::hi("Tim");
hello::hi("Tim");
hello::hi("Alan");
hello::hi("Alan");

A Simple "Hello" Object

Now we're using a full blown object that tracks greetings on a per-person basis.

hello.pm

package hello;

sub new {
	my($class, $name) = @_;
	my($self) = {
		'name' => $name,
		'count' => 0,
	};
	bless $self;
	return $self;
}


sub hi {
	my($self) = @_;
	$self->{'count'}++;
	my $name = $self->{'name'};
	print "Hello, $name (I've said hello to $name $self->{'count'} times)\n";
}
1;

main.pl

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

use FindBin;
use lib $FindBin::Bin;
use hello;

my $hello_alan = new hello("Alan");
my $hello_tim = new hello("Tim");
$hello_alan->hi();
$hello_tim->hi();
$hello_tim->hi();
$hello_alan->hi();
$hello_alan->hi();

Inheritance

This is a simple example of inheritance. The object "greeting" gives the greeting "Salutations". hello is based on greeting and shares much of its logic, but replaces the greeting with "Hello".

hello.pm

package hello;
use greeting;
@ISA=("greeting");

sub new {
	my($class, $name) = @_;
	my($self) = {
		'name' => $name,
		'count' => 0,
	};
	bless $self, (ref($class) || $class);
	return $self;
}

sub greeting { return "Hello"; }
1;

greeting.pm

package greeting;

sub new {
	my($class, $name) = @_;
	my($self) = {
		'name' => $name,
		'count' => 0,
	};
	bless $self, (ref($class) || $class);
	return $self;
}

sub greeting { return "Salutations"; }

sub hi {
	my($self) = @_;
	$self->{'count'}++;
	my $name = $self->{'name'};
	my $greeting = $self->greeting();
	print "$greeting, $name (I've greeted $name $self->{'count'} times)\n";
}
1;

main.pl

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

use FindBin;
use lib $FindBin::Bin;
use hello;

my $hello_alan = new hello("Alan");
my $hello_tim = new greeting("Tim");
$hello_alan->hi();
$hello_tim->hi();
$hello_tim->hi();
$hello_alan->hi();
$hello_alan->hi();

A simple greeting program in Ruby

main.rb

#! /usr/bin/ruby

class Hello
  def initialize(name)
    @name = name
    @count = 0
  end
  def hi
    @count = @count + 1
    puts "Hello, #{@name} (I've said hello to #{@name} #{@count} times)"
  end
end

hello_alan = Hello.new("Alan")
hello_tim = Hello.new("Tim")
hello_alan.hi
hello_tim.hi
hello_tim.hi
hello_alan.hi
hello_alan.hi

A simple greeting program in Python

main.py

#! /usr/bin/python

class hello(object):
	def __init__(self, name):
		self._count = 0
		self._name = name
	def hi(self):
		self._count += 1
		print "Hello, %s (I've said hello to %s %d times)" % \
			(self._name, self._name, self._count)

hello_alan = hello("Alan")
hello_tim = hello("Tim")

hello_alan.hi()
hello_tim.hi()
hello_tim.hi()
hello_alan.hi()
hello_alan.hi()

A simple greeting program in JavaScript

You can see this script in action here.

main.js

function hello(name) {
	this.name = name;
	this.count = 0;
	this.hi = function() {
		this.count++;
		document.write("Hello, " + this.name + " (I've said hello to " +
			this.name + " " + this.count + " times)\n");
	}
}

hello_alan = new hello("Alan");
hello_tim = new hello("Tim");

hello_alan.hi();
hello_tim.hi();
hello_tim.hi();
hello_alan.hi();
hello_alan.hi();