Snippets

sironekotoro Perlでダックタイピング

Created by sironekotoro

File duck_typing.pl Added

  • 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';
+}
+
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.