| 1 | package Sub::Multi; | ||||||||||||
| 2 | 1 | our $VERSION = '0.003'; | |||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use base 'Class::Multimethods::Pure'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Data::Bind 0.27; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | =head1 NAME | ||||||||||||
| 8 | |||||||||||||
| 9 | Sub::Multi - Data::Bind-based multi-sub dispatch | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||
| 12 | |||||||||||||
| 13 | my $multi_sub = Sub::Multi->new($sub1, $sub2, $sub3); | ||||||||||||
| 14 | |||||||||||||
| 15 | Now dispatch to the right subroutine, based on C<@args>. | ||||||||||||
| 16 | |||||||||||||
| 17 | $multi_sub->(@args); | ||||||||||||
| 18 | |||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||
| 20 | |||||||||||||
| 21 | Perl6 allows multiple subs and methods with the same name, differing only in | ||||||||||||
| 22 | their signature. | ||||||||||||
| 23 | |||||||||||||
| 24 | multi sub bar (Dog $foo) {?} | ||||||||||||
| 25 | multi sub bar (Cat $foo) {?} | ||||||||||||
| 26 | |||||||||||||
| 27 | Dispatching will happen based on the runtime signature of the subroutine or | ||||||||||||
| 28 | method call. | ||||||||||||
| 29 | |||||||||||||
| 30 | =head2 new | ||||||||||||
| 31 | |||||||||||||
| 32 | my $multi_sub = Sub::Multi->new($sub1, $sub2, $sub3); | ||||||||||||
| 33 | $multi_sub->(@args); | ||||||||||||
| 34 | |||||||||||||
| 35 | Build and return a code reference that will dispatch based on the Perl6 | ||||||||||||
| 36 | multi dispatch semantics. | ||||||||||||
| 37 | |||||||||||||
| 38 | I<TODO: Verify this statement:> Before the method is actually dispatched, | ||||||||||||
| 39 | a call to Data::Bind->sub_signature should be made to register the subroutine | ||||||||||||
| 40 | signature. | ||||||||||||
| 41 | |||||||||||||
| 42 | =cut | ||||||||||||
| 43 | |||||||||||||
| 44 | |||||||||||||
| 45 | 3 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 46 | 3 | my ($class, @subs) = @_; | |||||||||||
| 47 | 3 | ▶ | return bless sub { $class->dispatch(\@subs, @_) }, 'Sub::Multi::Method'; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | =head2 add_multi | ||||||||||||
| 51 | |||||||||||||
| 52 | my $multi_sub = Sub::Multi->add_multi($sub_name, \&sub ); | ||||||||||||
| 53 | $multi_sub->(@args); | ||||||||||||
| 54 | |||||||||||||
| 55 | Associates C<$sub_name> with C<\&sub>, and returns code reference | ||||||||||||
| 56 | that will dispatch appropriately. C<add_multi> can be called multiple | ||||||||||||
| 57 | times with the same C<$sub_name> to build a multi-dispatch method. | ||||||||||||
| 58 | |||||||||||||
| 59 | I<TODO: Verify this statement:> Before the method is actually dispatched, | ||||||||||||
| 60 | a call to Data::Bind->sub_signature should be made to register the subroutine | ||||||||||||
| 61 | signature. | ||||||||||||
| 62 | |||||||||||||
| 63 | =cut | ||||||||||||
| 64 | |||||||||||||
| 65 | 2 | ▶ | sub add_multi { | ||||||||||
|
Subroutine
add_multi: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 66 | 2 | my ($class, $name, $sub) = @_; | |||||||||||
| 67 | 2 | my $pkg = ((caller)[0]); | |||||||||||
| 68 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 2 | ▶ | my $subs = ${$pkg."::SUB_MULTI_REGISTRY"} ||= []; | ||||||||||
|
Condition100%2/2
${$pkg . '::SUB_MULTI_REGISTRY';} ||= []
MC/DC100%1/1
${$pkg . '::SUB_MULTI_REGISTRY';} ||= []
${$pkg . '::SUB_MULTI_REGISTRY';}
Truth table100%2/2
A
A= ${$pkg . '::SUB_MULTI_REGISTRY';}
| |||||||||||||
| 70 | 2 | push @$subs, $sub; | |||||||||||
| 71 | 1 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | 2 | *{$pkg."::$name"} = $class->new(@$subs); | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 8 | ▶ | sub dispatch { | ||||||||||
|
Subroutine
dispatch: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 76 | 8 | my $class = shift; | |||||||||||
| 77 | 8 | my $subs = shift; | |||||||||||
| 78 | 8 | my @compat; | |||||||||||
| 79 | 8 | for my $variant (@$subs) { | |||||||||||
| 80 | 16 | my $cv = Data::Bind::_get_cv($variant); | |||||||||||
| 81 | 16 | ▶ | push @compat, $variant if *$cv->{sig}->is_compatible( [ @{$_[0]} ], { %{$_[1]} } ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | } | ||||||||||||
| 83 | 8 | ▶ | die 'I hate vapour ware' unless @compat; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 6 | ▶ | while (@compat != 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 2 | die 'I hate ambiguous software'; | |||||||||||
| 86 | } | ||||||||||||
| 87 | 4 | goto $compat[0]; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 1 | 1; | |||||||||||
| 91 | |||||||||||||
| 92 | =head1 SEE ALSO | ||||||||||||
| 93 | |||||||||||||
| 94 | L<Data::Bind> | ||||||||||||
| 95 | |||||||||||||
| 96 | B<TODO: > Add a good reference to Perl6 multiple dispatch here. | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 AUTHORS | ||||||||||||
| 99 | |||||||||||||
| 100 | Chia-liang Kao E<lt>clkao@clkao.orgE<gt> | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 COPYRIGHT | ||||||||||||
| 103 | |||||||||||||
| 104 | Copyright 2006 by Chia-liang Kao and others. | ||||||||||||
| 105 | |||||||||||||
| 106 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 107 | under the same terms as Perl itself. | ||||||||||||
| 108 | |||||||||||||
| 109 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||