Package Info

perl-asa


Lets your class/object say it works like something else


Development/Libraries/Perl

Perl 5 doesn't natively support Java-style interfaces, and it doesn't support Perl 6 style roles either.

You can get both of these things in half a dozen different ways via various CPAN modules, but they usually require that you buy into "their way" of implementing your code.

Other have turned to "duck typing".

This is, for the most part, a fairly naive check that says "can you do this method", under the "if it looks like a duck, and quacks like a duck, then it must be a duck".

It assumes that if you have a '->quack' method, then they will treat you as a duck, because doing things like adding 'Duck' to your '@ISA' array means you are also forced to take their implementation.

There is, of course, a better way.

For better or worse, Perl's '->isa' functionality to determine if something is or is not a particular class/object is defined as a method, not a function, and so that means that as well as adding something to you '@ISA' array, so that Perl's 'UNIVERSAL::isa' method can work with it, you are also allowed to simply overload your own 'isa' method and answer directly whether or not you are something.

The simplest form of the idiom looks like this.

sub isa { return 1 if $[1] eq 'Duck'; shift->SUPER::isa(@); }

This reads "Check my type as normal, but if anyone wants to know if I'm a duck, then tell them yes".

Now, there are a few people that have argued that this is "lying" about your class, but this argument is based on the idea that '@ISA' is somehow more "real" than using the method directly.

It also assumes that what you advertise you implement needs to be in sync with the method resolution for any given function. But in the best and cleanest implementation of code, the API is orthogonal (although most often related) to the implementation.

And although '@ISA' is about implementation and API, overloading 'isa' to let you change your API is not at all bad when seen in this light.


License: Artistic-1.0 or GPL-1.0+
URL: http://search.cpan.org/dist/asa/

Categories

Releases

Package Version Update ID Released Package Hub Version Platforms Subpackages
1.03-bp150.2.4 info GA Release 2018-07-30 15
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa
1.03-bp151.2.12 info GA Release 2019-05-18 15 SP1
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa
1.03-bp151.3.1 info GA Release 2019-07-16 15 SP1
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa
1.03-bp152.3.14 info GA Release 2020-04-17 15 SP2
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa
1.03-bp153.1.12 info GA Release 2021-03-06 15 SP3
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa
1.03-bp154.1.24 info GA Release 2022-05-09 15 SP4)
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-asa