Created by sironekotoro 2018-08-10 View revision File duck_typing.pl Added Side-by-side diff More Ignore whitespace Hide word diff +use strict; +use warnings; +use feature qw/say/; + +# https://ja.wikipedia.org/wiki/ダック・タイピング + +unless (caller) { + say Duck->new->sound; # quack + say Cat->new->sound; # myaa +} + +package Duck; + +sub new { + my $class = shift; + return bless \{}, $class; +} + +sub sound { + return 'quack'; +} + +package Cat; + +sub new { + my $class = shift; + return bless \{}, $class; +} + +sub sound { + return 'myaa'; +} +