This file is indexed.

/usr/share/perl5/Specio/Declare.pm is in libspecio-perl 0.42-1.

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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package Specio::Declare;

use strict;
use warnings;

use parent 'Exporter';

our $VERSION = '0.42';

use Carp qw( croak );
use Specio::Coercion;
use Specio::Constraint::Simple;
use Specio::DeclaredAt;
use Specio::Helpers qw( install_t_sub _STRINGLIKE );
use Specio::Registry qw( internal_types_for_package register );

## no critic (Modules::ProhibitAutomaticExportation)
our @EXPORT = qw(
    anon
    any_can_type
    any_does_type
    any_isa_type
    coerce
    declare
    enum
    intersection
    object_can_type
    object_does_type
    object_isa_type
    union
);
## use critic

sub import {
    my $package = shift;

    # What the heck is this monstrosity?
    #
    # Moose version 2.0901 included a first pass at support for Specio. This
    # was based on Specio c. 0.06 when Specio itself still used
    # Moose. Unfortunately, recent changes to Specio broke this support and
    # the Moose core needs updating.
    #
    # However, stable versions of Moose have since shipped with a test that
    # attempts to test itself with Specio 0.07+. This was fine until I wanted
    # to release a non-TRIAL Specio.
    #
    # Once that's out, anyone installing Specio will cause future attempts to
    # install Moose to fail until Moose includes updated Specio support!
    # Breaking Moose is not acceptable, thus this mess.
    #
    # Note that since Moose 2.1207 this test was renamed and the Specio tests
    # actually run (and pass). We still need to leave this in here for quite
    # some time. People should be able to install Specio and then install an
    # older Moose indefinitely (or at least for a year or two).
    if (   $ENV{HARNESS_ACTIVE}
        && $0 =~ m{t[\\/]type_constraints[\\/]specio\.t$} ) {

        require Test::More;
        Test::More::plan( skip_all =>
                'These tests will not pass with this version of Specio' );
        exit 0;
    }

    my $caller = caller();

    $package->export_to_level( 1, $package, @_ );

    install_t_sub(
        $caller,
        internal_types_for_package($caller)
    );

    return;
}

sub declare {
    my $name = _STRINGLIKE(shift)
        or croak 'You must provide a name for declared types';
    my %p = @_;

    my $tc = _make_tc( name => $name, %p );

    register( scalar caller(), $name, $tc, 'exportable' );

    return $tc;
}

sub anon {
    return _make_tc(@_);
}

sub enum {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    require Specio::Constraint::Enum;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        values     => $p{values},
        type_class => 'Specio::Constraint::Enum',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub object_can_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    # This cannot be loaded earlier, since it loads Specio::Library::Builtins,
    # which in turn wants to load Specio::Declare (the current module).
    require Specio::Constraint::ObjectCan;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        methods    => $p{methods},
        type_class => 'Specio::Constraint::ObjectCan',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub object_does_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    my $caller = scalar caller();

    # If we are being called repeatedly with a single argument, then we don't
    # want to blow up because the type has already been declared. This would
    # force the user to use t() for all calls but the first, making their code
    # pointlessly more complicated.
    unless ( keys %p ) {
        if ( my $exists = internal_types_for_package($caller)->{$name} ) {
            return $exists;
        }
    }

    require Specio::Constraint::ObjectDoes;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        role => ( defined $p{role} ? $p{role} : $name ),
        type_class => 'Specio::Constraint::ObjectDoes',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub object_isa_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    my $caller = scalar caller();
    unless ( keys %p ) {
        if ( my $exists = internal_types_for_package($caller)->{$name} ) {
            return $exists;
        }
    }

    require Specio::Constraint::ObjectIsa;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        class => ( defined $p{class} ? $p{class} : $name ),
        type_class => 'Specio::Constraint::ObjectIsa',
    );

    register( $caller, $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub any_can_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    # This cannot be loaded earlier, since it loads Specio::Library::Builtins,
    # which in turn wants to load Specio::Declare (the current module).
    require Specio::Constraint::AnyCan;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        methods    => $p{methods},
        type_class => 'Specio::Constraint::AnyCan',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub any_does_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    my $caller = scalar caller();
    unless ( keys %p ) {
        if ( my $exists = internal_types_for_package($caller)->{$name} ) {
            return $exists;
        }
    }

    require Specio::Constraint::AnyDoes;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        role => ( defined $p{role} ? $p{role} : $name ),
        type_class => 'Specio::Constraint::AnyDoes',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub any_isa_type {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    my $caller = scalar caller();
    unless ( keys %p ) {
        if ( my $exists = internal_types_for_package($caller)->{$name} ) {
            return $exists;
        }
    }

    require Specio::Constraint::AnyIsa;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        class => ( defined $p{class} ? $p{class} : $name ),
        type_class => 'Specio::Constraint::AnyIsa',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub intersection {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    require Specio::Constraint::Intersection;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        %p,
        type_class => 'Specio::Constraint::Intersection',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub union {
    my $name;
    $name = shift if @_ % 2;
    my %p = @_;

    require Specio::Constraint::Union;

    my $tc = _make_tc(
        ( defined $name ? ( name => $name ) : () ),
        %p,
        type_class => 'Specio::Constraint::Union',
    );

    register( scalar caller(), $name, $tc, 'exportable' )
        if defined $name;

    return $tc;
}

sub _make_tc {
    my %p = @_;

    my $class = delete $p{type_class} || 'Specio::Constraint::Simple';

    $p{constraint}        = delete $p{where}   if exists $p{where};
    $p{message_generator} = delete $p{message} if exists $p{message};
    $p{inline_generator}  = delete $p{inline}  if exists $p{inline};

    return $class->new(
        %p,
        declared_at => Specio::DeclaredAt->new_from_caller(2),
    );
}

sub coerce {
    my $to = shift;
    my %p  = @_;

    $p{coercion}         = delete $p{using}  if exists $p{using};
    $p{inline_generator} = delete $p{inline} if exists $p{inline};

    return $to->add_coercion(
        Specio::Coercion->new(
            to => $to,
            %p,
            declared_at => Specio::DeclaredAt->new_from_caller(1),
        )
    );
}

1;

# ABSTRACT: Specio declaration subroutines

__END__

=pod

=encoding UTF-8

=head1 NAME

Specio::Declare - Specio declaration subroutines

=head1 VERSION

version 0.42

=head1 SYNOPSIS

    package MyApp::Type::Library;

    use parent 'Specio::Exporter';

    use Specio::Declare;
    use Specio::Library::Builtins;

    declare(
        'Foo',
        parent => t('Str'),
        where  => sub { $_[0] =~ /foo/i },
    );

    declare(
        'ArrayRefOfInt',
        parent => t( 'ArrayRef', of => t('Int') ),
    );

    my $even = anon(
        parent => t('Int'),
        inline => sub {
            my $type      = shift;
            my $value_var = shift;

            return $value_var . ' % 2 == 0';
        },
    );

    coerce(
        t('ArrayRef'),
        from  => t('Foo'),
        using => sub { [ $_[0] ] },
    );

    coerce(
        $even,
        from  => t('Int'),
        using => sub { $_[0] % 2 ? $_[0] + 1 : $_[0] },
    );

    # Specio name is DateTime
    any_isa_type('DateTime');

    # Specio name is DateTimeObject
    object_isa_type( 'DateTimeObject', class => 'DateTime' );

    any_can_type(
        'Duck',
        methods => [ 'duck_walk', 'quack' ],
    );

    object_can_type(
        'DuckObject',
        methods => [ 'duck_walk', 'quack' ],
    );

    enum(
        'Colors',
        values => [qw( blue green red )],
    );

    intersection(
        'HashRefAndArrayRef',
        of => [ t('HashRef'), t('ArrayRef') ],
    );

    union(
        'IntOrArrayRef',
        of => [ t('Int'), t('ArrayRef') ],
    );

=head1 DESCRIPTION

This package exports a set of type declaration helpers. Importing this package
also causes it to create a C<t> subroutine the caller.

=head1 SUBROUTINES

This module exports the following subroutines.

=head2 t('name')

This subroutine lets you access any types you have declared so far, as well as
any types you imported from another type library.

If you pass an unknown name, it throws an exception.

=head2 declare(...)

This subroutine declares a named type. The first argument is the type name,
followed by a set of key/value parameters:

=over 4

=item * parent => $type

The parent should be another type object. Specifically, it can be anything
which does the L<Specio::Constraint::Role::Interface> role. The parent can be a
named or anonymous type.

=item * where => sub { ... }

This is a subroutine which defines the type constraint. It will be passed a
single argument, the value to check, and it should return true or false to
indicate whether or not the value is valid for the type.

This parameter is mutually exclusive with the C<inline> parameter.

=item * inline => sub { ... }

This is a subroutine that is called to generate inline code to validate the
type. Inlining can be I<much> faster than simply providing a subroutine with
the C<where> parameter, but is often more complicated to get right.

The inline generator is called as a method on the type with one argument. This
argument is a I<string> containing the variable name to use in the generated
code. Typically this is something like C<'$_[0]'> or C<'$value'>.

The inline generator subroutine should return a I<string> of code representing
a single term, and it I<should not> be terminated with a semicolon. This
allows the inlined code to be safely included in an C<if> statement, for
example. You can use C<do { }> blocks and ternaries to get everything into one
term. Do not assign to the variable you are testing. This single term should
evaluate to true or false.

The inline generator is expected to include code to implement both the current
type and all its parents. Typically, the easiest way to do this is to write a
subroutine something like this:

  sub {
      my $self = shift;
      my $var  = shift;

      return $_[0]->parent->inline_check( $_[1] )
          . ' and more checking code goes here';
  }

This parameter is mutually exclusive with the C<where> parameter.

=item * message_generator => sub { ... }

A subroutine to generate an error message when the type check fails. The
default message says something like "Validation failed for type named Int
declared in package Specio::Library::Builtins
(.../Specio/blib/lib/Specio/Library/Builtins.pm) at line 147 in sub named (eval)
with value 1.1".

You can override this to provide something more specific about the way the
type failed.

The subroutine you provide will be called as a method on the type with two
arguments. The first is the description of the type (the bit in the message
above that starts with "type named Int ..." and ends with "... in sub named
(eval)". This description says what the thing is and where it was defined.

The second argument is the value that failed the type check, after any
coercions that might have been applied.

=back

=head2 anon(...)

This subroutine declares an anonymous type. It is identical to C<declare>
except that it expects a list of key/value parameters without a type name as
the first parameter.

=head2 coerce(...)

This declares a coercion from one type to another. The first argument should
be an object which does the L<Specio::Constraint::Role::Interface> role. This
can be either a named or anonymous type. This type is the type that the
coercion is I<to>.

The remaining arguments are key/value parameters:

=over 4

=item * from => $type

This must be an object which does the L<Specio::Constraint::Role::Interface>
role. This is type that we are coercing I<from>. Again, this can be either a
named or anonymous type.

=item * using => sub { ... }

This is a subroutine which defines the type coercion. It will be passed a
single argument, the value to coerce. It should return a new value of the type
this coercion is to.

This parameter is mutually exclusive with the C<inline> parameter.

=item * inline => sub { ... }

This is a subroutine that is called to generate inline code to perform the
coercion.

The inline generator is called as a method on the type with one argument. This
argument is a I<string> containing the variable name to use in the generated
code. Typically this is something like C<'$_[0]'> or C<'$value'>.

The inline generator subroutine should return a I<string> of code representing
a single term, and it I<should not> be terminated with a semicolon. This
allows the inlined code to be safely included in an C<if> statement, for
example. You can use C<do { }> blocks and ternaries to get everything into one
term. This single term should evaluate to the new value.

=back

=head1 DECLARATION HELPERS

This module also exports some helper subs for declaring certain kinds of types:

=head2 any_isa_type, object_isa_type

The C<any_isa_type> helper creates a type which accepts a class name or
object of the given class. The C<object_isa_type> helper creates a type
which only accepts an object of the given class.

These subroutines take a type name as the first argument. The remaining
arguments are key/value pairs. Currently this is just the C<class> key, which
should be a class name. This is the class that the type requires.

The type name argument can be omitted to create an anonymous type.

You can also pass just a single argument, in which case that will be used as
both the type's name and the class for the constraint to check.

=head2 any_does_type, object_does_type

The C<any_does_type> helper creates a type which accepts a class name or
object which does the given role. The C<object_does_type> helper creates a
type which only accepts an object which does the given role.

These subroutines take a type name as the first argument. The remaining
arguments are key/value pairs. Currently this is just the C<role> key, which
should be a role name. This is the class that the type requires.

This should just work (I hope) with roles created by L<Moose>, L<Mouse>, and
L<Moo> (using L<Role::Tiny>).

The type name argument can be omitted to create an anonymous type.

You can also pass just a single argument, in which case that will be used as
both the type's name and the role for the constraint to check.

=head2 any_can_type, object_can_type

The C<any_can_type> helper creates a type which accepts a class name or
object with the given methods. The C<object_can_type> helper creates a type
which only accepts an object with the given methods.

These subroutines take a type name as the first argument. The remaining
arguments are key/value pairs. Currently this is just the C<methods> key,
which can be either a string or array reference of strings. These strings are
the required methods for the type.

The type name argument can be omitted to create an anonymous type.

=head2 enum

This creates a type which accepts a string matching a given list of acceptable
values.

The first argument is the type name. The remaining arguments are key/value
pairs. Currently this is just the C<values> key. This should an array
reference of acceptable string values.

The type name argument can be omitted to create an anonymous type.

=head2 intersection

This creates a type which is the intersection of two or more other types. A
union only accepts values which match all of its underlying types.

The first argument is the type name. The remaining arguments are key/value
pairs. Currently this is just the C<of> key. This should an array
reference of types.

The type name argument can be omitted to create an anonymous type.

=head2 union

This creates a type which is the union of two or more other types. A union
accepts any of its underlying types.

The first argument is the type name. The remaining arguments are key/value
pairs. Currently this is just the C<of> key. This should an array
reference of types.

The type name argument can be omitted to create an anonymous type.

=head1 PARAMETERIZED TYPES

You can create a parameterized type by calling C<t> with additional
parameters, like this:

  my $arrayref_of_int = t( 'ArrayRef', of => t('Int') );

  my $arrayref_of_hashref_of_int = t(
      'ArrayRef',
      of => t(
          'HashRef',
          of => t('Int'),
      ),
  );

The C<t> subroutine assumes that if it receives more than one argument, it
should look up the named type and call C<< $type->parameterize(...) >> with
the additional arguments.

If the named type cannot be parameterized, it throws an error.

You can also call C<< $type->parameterize >> directly if needed. See
L<Specio::Constraint::Parameterizable> for details.

=head1 SUPPORT

Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.

I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.

=head1 SOURCE

The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.

=head1 AUTHOR

Dave Rolsky <autarch@urth.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2012 - 2017 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

The full text of the license can be found in the
F<LICENSE> file included with this distribution.

=cut