Skip to content

Instantly share code, notes, and snippets.

@msouth
Created October 30, 2014 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msouth/dcd00556b5a8dd7fb2a6 to your computer and use it in GitHub Desktop.
Save msouth/dcd00556b5a8dd7fb2a6 to your computer and use it in GitHub Desktop.
use strict;
#########################################
############# would be in Animal.pm normally
#########################################
package Animal;
sub new {
my $package = shift;
# this 'bless' call tells that anonymous hash "if someone calls ->foo on you, look in $package for a sub named 'foo',
# and call it, with yourself as the first argument"
bless {age=>0,}, $package;
}
sub speak {
print "'Primal Grunt'\n";
}
sub shed {
print "* some fur drops to the ground *\n";
}
# common idiom for a getter/setter. "get" the value with $obj->age(); "set" the value with $obj->age($value);
sub age {
my $self = shift;
if (@_) {
$self->{age} = shift;
}
return $self->{age};
}
sub have_birthday {
my $self = shift;
$self->age( $self->age() + 1 );
}
#########################################
#########################################
############# would be in Dog.pm normally
#########################################
package Dog;
use base qw/Animal/;
sub speak {
print "'Woof'\n";
}
#########################################
#########################################
############# would be in Snake.pm normally
#########################################
package Snake;
use base qw/Animal/;
sub speak {
print "'Hissssss'\n";
}
sub shed {
print "* A dried, empty shell of a snakeskin is left ominously on your porch *\n";
}
#########################################
#########################################
############ would be in FireDog.pm normally
#########################################
package FireDog;
use base qw/Dog/;
sub shed {
my $self = shift;
# the SUPER 'package' means "look in my inheritance hierarchy for something called [in this case] 'shed', and call it, (with me as the
# first argument)
$self->SUPER::shed;
print "* ...and then spontaneously combusts *\n";
}
#########################################
#########################################
############ would just be the start of the file, normally, 'package main' would be implied in that case
#########################################
package main;
# 'Bareword->new' looks for a package called "Bareword", and calls that package's "new()" subroutine, with 'Bareword' as the first argument
my $generic = Animal->new();
print "*******\n$generic\n\n";
$generic->speak;
$generic->shed;
print "The generic animal is ". $generic->age(). " years old.\n";
$generic->have_birthday;
print "Haivng had a birthday, the generic animal is ". $generic->age(). " year(s) old.\n";
# This will call the Dog package's new() subroutine--but it doesn't have one, so it goes up to its "base" of Animal,
# and call's Animal's new(), but with 'Dog' as the first argument.
my $dog = Dog->new();
print "*******\n$dog\n\n";
$dog->speak;
$dog->shed;
my $snake = Snake->new();
print "*******\n$snake\n\n";
$snake->speak;
$snake->shed;
my $fire_dog = FireDog->new();
print "*******\n$fire_dog\n\n";
$fire_dog->speak;
$fire_dog->shed;
@msouth
Copy link
Author

msouth commented Oct 30, 2014

[mjsouth@msouth-ns-air ~ ]$ perl whatever.pl 
*******
Animal=HASH(0x7ff084004268)

'Primal Grunt'
* some fur drops to the ground *
The generic animal is 0 years old.
Haivng had a birthday, the generic animal is 1 year(s) old.
*******
Dog=HASH(0x7ff0840113c0)

'Woof'
* some fur drops to the ground *
*******
Snake=HASH(0x7ff084011cc0)

'Hissssss'
* A dried, empty shell of a snakeskin is left ominously on your porch *
*******
FireDog=HASH(0x7ff085005660)

'Woof'
* some fur drops to the ground *
* ...and then spontaneously combusts *

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment