1asa(3)                User Contributed Perl Documentation               asa(3)
2
3
4

NAME

6       asa - Lets your class/object say it works like something else
7

VERSION

9       version 1.04
10

SYNOPSIS

12         #########################################
13         # Your Code
14
15         package My::WereDuck;
16
17         use base 'My::Lycanthrope';
18         use asa  'Duck';
19
20         sub quack {
21             return "Hi! errr... Quack!";
22         }
23
24         ################################################
25         # Their Code
26
27         sub strangle {
28             my $duck = shift;
29             unless ( $duck->isa('Duck') ) {
30                 die "We only strangle ducks";
31             }
32             print "Farmer Joe wrings the duck's neck\n";
33             print "Said the duck, '" . $duck->quack . "'\n";
34             print "We ate well that night.\n";
35         }
36

DESCRIPTION

38       Perl 5 doesn't natively support Java-style interfaces, and it doesn't
39       support Perl 6 style roles either.
40
41       You can get both of these things in half a dozen different ways via
42       various CPAN modules, but they usually require that you buy into "their
43       way" of implementing your code.
44
45       Other have turned to "duck typing".
46
47       This is, for the most part, a fairly naive check that says "can you do
48       this method", under the "if it looks like a duck, and quacks like a
49       duck, then it must be a duck".
50
51       It assumes that if you have a "->quack" method, then they will treat
52       you as a duck, because doing things like adding "Duck" to your @ISA
53       array means you are also forced to take their implementation.
54
55       There is, of course, a better way.
56
57       For better or worse, Perl's "->isa" functionality to determine if
58       something is or is not a particular class/object is defined as a
59       method, not a function, and so that means that as well as adding
60       something to you @ISA array, so that Perl's "UNIVERSAL::isa" method can
61       work with it, you are also allowed to simply overload your own "isa"
62       method and answer directly whether or not you are something.
63
64       The simplest form of the idiom looks like this.
65
66         sub isa {
67             return 1 if $_[1] eq 'Duck';
68             shift->SUPER::isa(@_);
69         }
70
71       This reads "Check my type as normal, but if anyone wants to know if I'm
72       a duck, then tell them yes".
73
74       Now, there are a few people that have argued that this is "lying" about
75       your class, but this argument is based on the idea that @ISA is somehow
76       more "real" than using the method directly.
77
78       It also assumes that what you advertise you implement needs to be in
79       sync with the method resolution for any given function. But in the best
80       and cleanest implementation of code, the API is orthogonal (although
81       most often related) to the implementation.
82
83       And although @ISA is about implementation and API, overloading "isa" to
84       let you change your API is not at all bad when seen in this light.
85
86   What does asa.pm do?
87       Much as base provides convenient syntactic sugar for loading your
88       parent class and setting @ISA, this pragma will provide convenient
89       syntactic sugar for creating your own custom overloaded isa functions.
90
91       Beyond just the idiom above, it implements various workarounds for some
92       edge cases, so you don't have to, and allows clear separation of
93       concerns.
94
95       You should just be able to use it, and if something ever goes wrong,
96       then it's my fault, and I'll fix it.
97
98   What doesn't asa.pm do?
99       In Perl, highly robust introspection is really hard. Which is why most
100       modules that provide some level of interface functionality require you
101       to explicitly define them in multiple classes, and start to tread on
102       your toes.
103
104       This class does not do any strict enforcement of interfaces. 90% of the
105       time, what you want to do, and the methods you need to implement, are
106       going to be pretty obvious, so it's your fault if you don't provide
107       them.
108
109       But at least this way, you can implement them however you like, and
110       "asa" will just take care of the details of safely telling everyone
111       else you are a duck :)
112
113   What if a Duck method clashes with a My::Package method?
114       Unlike Perl 6, which implements a concept called "multi-methods", Perl
115       5 does not have a native approach to solving the problem of "API
116       collision".
117
118       Originally from the Java/C++ world, the problem of overcoming language
119       API limitations can be done through the use of one of several "design
120       patterns".
121
122       For you, the victim of API collision, you might be interested in the
123       "Adapter" pattern.
124
125       For more information on implementing the Adapter pattern in Perl, see
126       Class::Adapter, which provides a veritable toolkit for creating an
127       implementation of the Adapter pattern which can solve your problem.
128

SEE ALSO

130       <http://ali.as/>
131

SUPPORT

133       Bugs may be submitted through the RT bug tracker
134       <https://rt.cpan.org/Public/Dist/Display.html?Name=asa> (or
135       bug-asa@rt.cpan.org <mailto:bug-asa@rt.cpan.org>).
136

AUTHOR

138       Adam Kennedy, <adamk@cpan.org>
139

CONTRIBUTORS

141       ·   Adam Kennedy <adam@ali.as>
142
143       ·   Karen Etheridge <ether@cpan.org>
144
146       This software is copyright (c) 2006 by Adam Kennedy.
147
148       This is free software; you can redistribute it and/or modify it under
149       the same terms as the Perl 5 programming language system itself.
150
151
152
153perl v5.30.1                      2020-01-30                            asa(3)
Impressum