This file is indexed.

/usr/share/perl5/MooseX/Types/VariantTable.pm is in libmoosex-types-varianttable-perl 0.04-2.

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/perl

package MooseX::Types::VariantTable;
use Moose;

use Hash::Util::FieldHash qw(idhash);
use Scalar::Util qw(refaddr);

use Moose::Util::TypeConstraints;

use namespace::clean -except => 'meta';

with qw(MooseX::Clone);

use Carp qw(croak);

our $VERSION = "0.04";

has _sorted_variants => (
    traits => [qw(NoClone)],
    #isa => "ArrayRef[ArrayRef[HashRef]]",
    is  => "ro",
    lazy_build => 1,
);

has variants => (
    traits => [qw(Copy)],
    isa => "ArrayRef[HashRef]",
    is  => "rw",
    init_arg => undef,
    default  => sub { [] },
    trigger  => sub { $_[0]->_clear_sorted_variants },
);

has ambigious_match_callback => (
    is      => 'ro',
    isa     => 'CodeRef',
    default => sub {
        sub {
            my ($self, $value, @matches) = @_;
            croak "Ambiguous match " . join(", ", map { $_->{type} } @matches);
        };
    },
);

sub BUILD {
    my ( $self, $params ) = @_;

    if ( my $variants = $params->{variants} ) {
        foreach my $variant ( @$variants ) {
            $self->add_variant( @{ $variant }{qw(type value)} );
        }
    }
}

sub merge {
    my ( @selves ) = @_; # our @selves reads better =/

    my $self = $selves[0];

    return ( ref $self )->new(
        variants => [ map { @{ $_->variants } } @selves ],
    );
}

sub has_type {
    my ( $self, $type_or_name ) = @_;

    my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
        or croak "No such type constraint: $type_or_name";

    foreach my $existing_type ( map { $_->{type} } @{ $self->variants } ) {
        return 1 if $type->equals($existing_type);
    }

    return;
}

sub has_parent {
    my ( $self, $type_or_name ) = @_;

    my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
        or croak "No such type constraint: $type_or_name";

    foreach my $existing_type ( map { $_->{type} } @{ $self->variants } ) {
        return 1 if $type->is_subtype_of($existing_type);
    }

    return;
}

sub add_variant {
    my ( $self, $type_or_name, $value ) = @_;

    croak "Duplicate variant entry for $type_or_name"
        if $self->has_type($type_or_name);

    my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
        or croak "No such type constraint: $type_or_name";

    my $entry = { type => $type, value => $value };

    push @{ $self->variants }, $entry;

    $self->_clear_sorted_variants;

    return;
}

sub remove_variant {
    my ( $self, $type_or_name, $value ) = @_;

    my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
        or croak "No such type constraint: $type_or_name";

    my $list = $self->variants;

    @$list = grep { not $_->{type}->equals($type) } @$list;

    $self->_clear_sorted_variants;

    return;
}

sub _build__sorted_variants {
    my $self = shift;

    my @entries = @{ $self->variants };

    idhash my %out;

    foreach my $entry ( @entries ) {
        $out{$entry} = [];
        foreach my $other ( @entries ) {
            next if refaddr($entry) == refaddr($other);

            if ( $other->{type}->is_subtype_of($entry->{type}) ) {
                push @{ $out{$entry} }, $other;
            }
        }
    }

    my @sorted;

    while ( keys %out ) {
        my @slot;

        foreach my $entry ( @entries ) {
            if ( $out{$entry} and not @{ $out{$entry} } ) {
                push @slot, $entry;
                delete $out{$entry};
            }
        }

        idhash my %filter;
        @filter{@slot} = ();

        foreach my $entry ( @entries ) {
            if ( my $out = $out{$entry} ) {
                @$out = grep { not exists $filter{$_} } @$out;
            }
        }

        push @sorted, \@slot;
    }

    return \@sorted;
}

sub find_variant {
    my ( $self, @args ) = @_;

    if ( my $entry = $self->_find_variant(@args) ) {
        if ( wantarray ) {
            return @{ $entry }{qw(value type)};
        } else {
            return $entry->{value};
        }
    }

    return;
}

sub _find_variant {
    my ( $self, $value ) = @_;

    foreach my $slot ( @{ $self->_sorted_variants } ) {
        my @matches;
        foreach my $entry ( @$slot ) {
            if ( $entry->{type}->check($value) ) {
                push @matches, $entry;
            }
        }
        if ( @matches == 1 ) {
            return $matches[0];
        } elsif ( @matches > 1 ) {
            $self->ambigious_match_callback->($self, $value, @matches);
        }
    }

    return;
}

sub dispatch {
    my $self = shift;
    my $value = $_[0];

    if ( my $result = $self->find_variant($value) ) {
        if ( (ref($result)||'') eq 'CODE' ) {
            goto &$result;
        } else {
            return $result;
        }
    }

    return;
}

__PACKAGE__

__END__

=pod

=head1 NAME

MooseX::Types::VariantTable - Type constraint based variant table

=head1 SYNOPSIS

    # see also MooseX::Types::VariantTable::Declare for a way to
    # declare variant table based methods

	use MooseX::Types::VariantTable;

    my $dispatch_table = MooseX::Types::VariantTable->new(
        variants => [
            { type => "Foo", value => \&foo_handler },
            { type => "Bar", value => \&bar_handler },
            { type => "Item", value => \&fallback },
        ],
    );

    # look up the correct handler for $thingy based on the type constraints it passes
    my $entry = $dispatch_table->find_variant($thingy);

    # or use the 'dispatch' convenience method if the entries are code refs
    $dispatch_table->dispatch( $thingy, @args );

=head1 DESCRIPTION

This object implements a simple dispatch table based on L<Moose> type constraints.

Subtypes will be checked before their parents, meaning that the order of the
declaration does not matter.

This object is used internally by L<Moose::Meta::Method::VariantTable> and
L<MooseX::Types::VariantTable::Declare> to provide primitive multi
sub support.

=head1 ATTRIBUTES

=head2 ambigious_match_callback

A code reference that'll be executed when find_variant found more than one
matching variant for a value. It defaults to something that simply croaks with
an error message like this:

  Ambiguous match %s

where %s contains a list of stringified types that matched.

=head1 METHODS

=over 4

=item new

=item add_variant $type, $value

Registers C<$type>, such that C<$value> will be returned by C<find_variant> for
items passing $type.

Subtyping is respected in the table.

=item find_variant $value

Returns the registered value for the most specific type that C<$value> passes.

=item dispatch $value, @args

A convenience method for when the registered values are code references.

Calls C<find_variant> and if the result is a code reference, it will C<goto>
this code reference with the value and any additional arguments.

=item has_type $type

Returns true if an entry for C<$type> is registered.

=item has_parent $type

Returns true if a parent type of C<$type> is registered.

=back

=head1 TODO

The meta method composes in multiple inheritence but not yet with roles due to
extensibility issues with the role application code.

When L<Moose::Meta::Role> can pluggably merge methods variant table methods can
gain role composition.

=head1 AUTHOR

Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>

Florian Ragwitz E<lt>rafl@debian.orgE<gt>

=head1 COPYRIGHT

	Copyright (c) 2008 Yuval Kogman. All rights reserved
	This program is free software; you can redistribute
	it and/or modify it under the same terms as Perl itself.

=cut