Last active
August 29, 2015 13:57
-
-
Save hinrik/9590349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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