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.
sub hi { print "Hello, world\n"; } 1;
#! /usr/bin/perl use strict; use warnings; use hello; hi();
#! /usr/bin/perl use strict; use warnings; use FindBin; use lib $FindBin::Bin; use hello; hi();
#! /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"; }
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.
package hello; my $counter = 0; sub hi { my($name) = @_; $counter++; print "Hello, $name (I've said hello to $name $counter times)\n"; } 1;
#! /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");
Now we're using a full blown object that tracks greetings on a per-person basis.
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;
#! /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();
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".
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;
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;
#! /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();
#! /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
#! /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()
You can see this script in action here.
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();