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