Class::Multimethods

Support multimethods and function overloading in Perl
Download

Class::Multimethods Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Damian Conway
  • Publisher web site:
  • http://search.cpan.org/~dconway/

Class::Multimethods Tags


Class::Multimethods Description

Support multimethods and function overloading in Perl Class::Multimethods is a Perl module that offers multimethods and function overloading in Perl.SYNOPSIS # IMPORT THE multimethod DECLARATION SUB... use Class::Multimethods; # DECLARE VARIOUS MULTIMETHODS CALLED find... # 1. DO THIS IF find IS CALLED WITH A Container REF AND A Query REF... multimethod find => (Container, Query) => sub { $_->findquery($_) }; # 2. DO THIS IF find IS CALLED WITH A Container REF AND A Sample REF... multimethod find => (Container, Sample) => sub { $_->findlike($_) }; # 3. DO THIS IF find IS CALLED WITH AN Index REF AND A Word REF... multimethod find => (Index, Word) => sub { $_->lookup_word($_) }; # 4. DO THIS IF find IS CALLED WITH AN Index REF AND A qr// PATTERN multimethod find => (Index, Regexp) => sub { $_->lookup_rx($_) }; # 5. DO THIS IF find IS CALLED WITH AN Index REF AND A NUMERIC SCALAR multimethod find => (Index, '#') => sub { $_->lookup_elem($_) }; # 6. DO THIS IF find IS CALLED WITH AN Index REF AND A NON-NUMERIC SCALAR multimethod find => (Index, '$') => sub { $_->lookup_str($_) }; # 7. DO THIS IF find IS CALLED WITH AN Index REF AND AN UNBLESSED ARRAY REF # (NOTE THE RECURSIVE CALL TO THE find MULTIMETHOD) multimethod find => (Index, ARRAY) => sub { map { find($_,$_) } @{$_} }; # SET UP SOME OBJECTS... my $cntr = new Container ('./datafile'); my $indx = $cntr->get_index(); # ...AND SOME INHERITANCE... @BadWord::ISA = qw( Word ); my $badword = new BadWord("fubar"); # ...AND EXERCISE THEM... print find($cntr, new Query('cpan OR Perl')); # CALLS 1. print find($cntr, new Example('by a committee')); # CALLS 2. print find($indx, new Word('sugar')); # CALLS 3. print find($indx, $badword); # CALLS 3. print find($indx, qr/another brick in the Wall/); # CALLS 4. print find($indx, 7); # CALLS 5. print find($indx, 'But don't do that.'); # CALLS 6. print find($indx, ); # CALLS 7, # THEN 5 & 6.The Class:Multimethod module exports a subroutine (&multimethod) that can be used to declare other subroutines that are dispatched using a algorithm different from the normal Perl subroutine or method dispatch mechanism.Normal Perl subroutines are dispatched by finding the appropriately-named subroutine in the current (or specified) package and calling that. Normal Perl methods are dispatched by attempting to find the appropriately-named subroutine in the package into which the invoking object is blessed or, failing that, recursively searching for it in the packages listed in the appropriate @ISA arrays.Class::Multimethods multimethods are dispatched quite differently. The dispatch mechanism looks at the classes or types of each argument to the multimethod (by calling ref on each) and determines the "closest" matching variant of the multimethod, according to the argument types specified in the variants' definitions (see "Finding the "nearest" multimethod" for a definition of "closest").The result is something akin to C++'s function overloading, but more intelligent, since multimethods take the inheritance relationships of each argument into account. Another way of thinking of the mechanism is that it performs polymorphic dispatch on every argument of a method, not just the first. Requirements: · Perl


Class::Multimethods Related Software