« blib/lib/Class/MakeMethods/Standard/Universal.pm
↑ Summary
blib/lib/Class/MakeMethods/Standard/Array.pm »
| 1 | =head1 NAME | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | Class::MakeMethods::Composite - Make extensible compound methods | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | package MyObject; | ||||||||||||||||||
| 9 | use Class::MakeMethods::Composite::Hash ( | ||||||||||||||||||
| 10 | new => 'new', | ||||||||||||||||||
| 11 | scalar => [ 'foo', 'bar' ], | ||||||||||||||||||
| 12 | array => 'my_list', | ||||||||||||||||||
| 13 | hash => 'my_index', | ||||||||||||||||||
| 14 | ); | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | This document describes the various subclasses of Class::MakeMethods | ||||||||||||||||||
| 20 | included under the Composite::* namespace, and the method types each | ||||||||||||||||||
| 21 | one provides. | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | The Composite subclasses provide a parameterized set of method-generation | ||||||||||||||||||
| 24 | implementations. | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | Subroutines are generated as closures bound to a hash containing | ||||||||||||||||||
| 27 | the method name and additional parameters, including the arrays of subroutine references that will provide the method's functionality. | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | =head2 Calling Conventions | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | When you C<use> this package, the method names you provide | ||||||||||||||||||
| 33 | as arguments cause subroutines to be generated and installed in | ||||||||||||||||||
| 34 | your module. | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | See L<Class::MakeMethods::Standard/"Calling Conventions"> for more information. | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head2 Declaration Syntax | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | To declare methods, pass in pairs of a method-type name followed | ||||||||||||||||||
| 41 | by one or more method names. | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | Valid method-type names for this package are listed in L<"METHOD | ||||||||||||||||||
| 44 | GENERATOR TYPES">. | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | See L<Class::MakeMethods::Standard/"Declaration Syntax"> and L<Class::MakeMethods::Standard/"Parameter Syntax"> for more information. | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | =cut | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | package Class::MakeMethods::Composite; | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 7 | $VERSION = 1.000; | |||||||||||||||||
| 53 | 7 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 54 | 7 | ▶ | use Class::MakeMethods '-isasubclass'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 55 | 7 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | ######################################################################## | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | =head2 About Composite Methods | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | The methods generated by Class::MakeMethods::Composite are assembled | ||||||||||||||||||
| 62 | from groups of "fragment" subroutines, each of which provides some | ||||||||||||||||||
| 63 | aspect of the method's behavior. | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | You can add pre- and post- operations to any composite method. | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | package MyObject; | ||||||||||||||||||
| 68 | use Class::MakeMethods::Composite::Hash ( | ||||||||||||||||||
| 69 | new => 'new', | ||||||||||||||||||
| 70 | scalar => [ | ||||||||||||||||||
| 71 | 'foo' => { | ||||||||||||||||||
| 72 | 'pre_rules' => [ | ||||||||||||||||||
| 73 | sub { | ||||||||||||||||||
| 74 | # Don't automatically convert list to array-ref | ||||||||||||||||||
| 75 | croak "Too many arguments" if ( scalar @_ > 2 ); | ||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | ], | ||||||||||||||||||
| 78 | 'post_rules' => [ | ||||||||||||||||||
| 79 | sub { | ||||||||||||||||||
| 80 | # Don't let anyone see my credit card number! | ||||||||||||||||||
| 81 | ${(pop)->{result}} =~ s/\d{13,16}/****/g; | ||||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | ], | ||||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | ], | ||||||||||||||||||
| 86 | ); | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =cut | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | 7 | ▶ | use vars qw( $Method ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 0 | ▶ | sub CurrentMethod { | ||||||||||||||||
|
Subroutine
CurrentMethod: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 93 | 0 | $Method; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 8 | ▶ | sub CurrentResults { | ||||||||||||||||
|
Subroutine
CurrentResults: called CC=5 SCAR=17.6
Pod
undocumented
| |||||||||||||||||||
| 97 | 8 | my $package = shift; | |||||||||||||||||
| 98 | 8 | ▶ | if ( ! scalar @_ ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 99 | ( ! $Method->{result} ) ? () : | ||||||||||||||||||
| 100 | 0 | ( ref($Method->{result}) eq 'ARRAY' ) ? @{$Method->{result}} : | |||||||||||||||||
| 101 | 7 | ▶ | ${$Method->{result}}; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 102 | } elsif ( scalar @_ == 1) { | ||||||||||||||||||
| 103 | 1 | my $value = shift; | |||||||||||||||||
| 104 | 1 | $Method->{result} = \$value; | |||||||||||||||||
| 105 | 1 | $value | |||||||||||||||||
| 106 | } else { | ||||||||||||||||||
| 107 | 0 | my @value = @_; | |||||||||||||||||
| 108 | 0 | $Method->{result} = \@value; | |||||||||||||||||
| 109 | 0 | @value; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 29 | ▶ | sub _build_composite { | ||||||||||||||||
|
Subroutine
_build_composite: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 114 | 29 | my $class = shift; | |||||||||||||||||
| 115 | 29 | my $fragments = shift; | |||||||||||||||||
| 116 | map { | ||||||||||||||||||
| 117 | 29 | my $method = $_; | |||||||||||||||||
| 118 | 52 | my @fragments = @{ $fragments->{''} }; | |||||||||||||||||
| 119 | 52 | foreach my $flagname ( grep $method->{$_}, qw/ permit modifier / ) { | |||||||||||||||||
| 120 | 12 | my $value = $method->{$flagname}; | |||||||||||||||||
| 121 | 12 | ▶ | my $fragment = $fragments->{$value} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | or croak "Unsupported $flagname flag '$value'"; | ||||||||||||||||||
| 123 | 12 | push @fragments, @$fragment; | |||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | 52 | _bind_composite( $method, @fragments ); | |||||||||||||||||
| 126 | } $class->_get_declarations(@_) | ||||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 52 | ▶ | sub _assemble_fragments { | ||||||||||||||||
|
Subroutine
_assemble_fragments: called CC=5 SCAR=16.8
| |||||||||||||||||||
| 130 | 52 | my $method = shift; | |||||||||||||||||
| 131 | 52 | my @fragments = @_; | |||||||||||||||||
| 132 | 52 | ▶ | while ( scalar @fragments ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 133 | 116 | my ($rule, $sub) = splice( @fragments, 0, 2 ); | |||||||||||||||||
| 134 | 116 | ▶ | if ( $rule =~ s/\A\+// ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 135 | 60 | unshift @{$method->{"${rule}_rules"}}, $sub | |||||||||||||||||
| 136 | } elsif ( $rule =~ s/\+\Z// ) { | ||||||||||||||||||
| 137 | 0 | push @{$method->{"${rule}_rules"}}, $sub | |||||||||||||||||
| 138 | } elsif ( $rule =~ /\A\w+\Z/ ) { | ||||||||||||||||||
| 139 | 56 | @{$method->{"${rule}_rules"}} = $sub; | |||||||||||||||||
| 140 | } else { | ||||||||||||||||||
| 141 | 0 | croak "Unsupported rule type '$rule'" | |||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | 52 | ▶ | sub _bind_composite { | ||||||||||||||||
|
Subroutine
_bind_composite: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 147 | 52 | my $method = shift; | |||||||||||||||||
| 148 | 52 | _assemble_fragments( $method, @_ ); | |||||||||||||||||
| 149 | 52 | ▶ | if ( my $subs = $method->{"init_rules"} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 150 | 52 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 151 | 52 | &$sub( $method ); | |||||||||||||||||
| 152 | } | ||||||||||||||||||
| 153 | } | ||||||||||||||||||
| 154 | 112 | ▶ | $method->{name} => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=18 SCAR=29.0
| |||||||||||||||||||
| 155 | 112 | local $Method = $method; | |||||||||||||||||
| 156 | 112 | local $Method->{args} = [ @_ ]; | |||||||||||||||||
| 157 | 112 | local $Method->{result}; | |||||||||||||||||
| 158 | 112 | local $Method->{scratch}; | |||||||||||||||||
| 159 | # Strange but true: you can local a hash-value in hash that's not | ||||||||||||||||||
| 160 | # a package variable. Confirmed in in 5.004, 5.005, 5.6.0. | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 112 | local $Method->{wantarray} = wantarray; | |||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | 112 | ▶ | if ( my $subs = $Method->{"pre_rules"} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 165 | 9 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 166 | 14 | &$sub( @{$Method->{args}}, $Method ); | |||||||||||||||||
| 167 | } | ||||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | 110 | ▶ | my $subs = $Method->{"do_rules"} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 171 | or Carp::confess("No operations provided for $Method->{name}"); | ||||||||||||||||||
| 172 | 110 | ▶ | if ( ! defined $Method->{wantarray} ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 173 | 24 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 174 | 24 | ▶ | last if $Method->{result}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 175 | 24 | &$sub( @{$Method->{args}}, $Method ); | |||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | } elsif ( ! $Method->{wantarray} ) { | ||||||||||||||||||
| 178 | 83 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 179 | 83 | ▶ | last if $Method->{result}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 180 | 83 | my $value = &$sub( @{$Method->{args}}, $Method ); | |||||||||||||||||
| 181 | 83 | ▶ | if ( defined $value ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 182 | 73 | $Method->{result} = \$value; | |||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | } | ||||||||||||||||||
| 185 | } else { | ||||||||||||||||||
| 186 | 3 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 187 | 3 | ▶ | last if $Method->{result}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 188 | 3 | my @value = &$sub( @{$Method->{args}}, $Method ); | |||||||||||||||||
| 189 | 3 | ▶ | if ( scalar @value ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 190 | 3 | $Method->{result} = \@value; | |||||||||||||||||
| 191 | } | ||||||||||||||||||
| 192 | } | ||||||||||||||||||
| 193 | } | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | 110 | ▶ | if ( my $subs = $Method->{"post_rules"} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 196 | 5 | foreach my $sub ( @$subs ) { | |||||||||||||||||
| 197 | 5 | &$sub( @{$Method->{args}}, $Method ); | |||||||||||||||||
| 198 | } | ||||||||||||||||||
| 199 | } | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | ( ! $Method->{result} ) ? () : | ||||||||||||||||||
| 202 | 3 | ( ref($Method->{result}) eq 'ARRAY' ) ? @{$Method->{result}} : | |||||||||||||||||
| 203 | 110 | ▶ | ${$Method->{result}}; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 204 | } | ||||||||||||||||||
| 205 | 52 | } | |||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | ######################################################################## | ||||||||||||||||||
| 208 | |||||||||||||||||||
| 209 | =head1 SEE ALSO | ||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | See L<Class::MakeMethods> for general information about this distribution. | ||||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | For distribution, installation, support, copyright and license | ||||||||||||||||||
| 214 | information, see L<Class::MakeMethods::Docs::ReadMe>. | ||||||||||||||||||
| 215 | |||||||||||||||||||
| 216 | =cut | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | 7 | 1; | |||||||||||||||||