Skip to content

Instantly share code, notes, and snippets.

@hinrik
Last active August 29, 2015 13:57
Show Gist options
  • Save hinrik/9590349 to your computer and use it in GitHub Desktop.
Save hinrik/9590349 to your computer and use it in GitHub Desktop.
package Location;
sub new {
my ($class, %args) = @_;
my $subclass = "Location::$args{location_type}";
return bless \%args, $subclass;
}
package Location::Country;
use base 'Location';
sub new {
my ($class, %args) = @_;
my $self = Location->new(%args);
# do something Country-specific
return $self;
}
sub description { "a country called $_[0]{name}" }
package Location::City;
use base 'Location';
sub new {
my ($class, %args) = @_;
my $self = Location->new(%args);
# do something City-specific
return $self;
}
sub description { "a city called $_[0]{name}" }
package main;
# any users of Location->new will get back an object that isa("Location")
# which might or might not be an instance of a derivative class
my $country = Location->new(
type => 'country',
name => 'Foo',
);
my $country = Location->new(
type => 'city',
name => 'Bar',
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment