File: subtype.pl

package info (click to toggle)
libmouse-perl 2.5.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,160 kB
  • sloc: perl: 14,614; ansic: 218; makefile: 8
file content (58 lines) | stat: -rwxr-xr-x 1,340 bytes parent folder | download | duplicates (7)
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
#!perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;

for my $klass (qw/Moose Mouse/) {
    eval qq{
        package ${klass}One;
        use $klass;
        use ${klass}::Util::TypeConstraints;

        subtype 'NaturalNumber', as 'Int', where { \$_ > 0 };

        has n => (
            is  => 'rw',
            isa => 'NaturalNumber',
        );
        no $klass;
        __PACKAGE__->meta->make_immutable;
    };
    die $@ if $@;
}

#use Data::Dumper;
#$Data::Dumper::Deparse = 1;
#$Data::Dumper::Indent  = 1;
#print Mouse::Util::TypeConstraints::find_type_constraint('NaturalNumber')->dump(3);
#print Moose::Util::TypeConstraints::find_type_constraint('NaturalNumber')->dump(3);

print "Class::MOP: $Class::MOP::VERSION\n";
print "Moose:      $Moose::VERSION\n";
print "Mouse:      $Mouse::VERSION\n";
print "---- new\n";
cmpthese(
    -1 => {
        map { my $x = $_; $_ => sub { $x->new(n => 3) } }
        map { "${_}One" }
        qw/Moose Mouse/
    }
);

print "---- new,set\n";
cmpthese(
    -1 => {
        map { my $y = $_; $_ => sub { $y->new(n => 3)->n(5) } }
        map { "${_}One" }
        qw/Moose Mouse/
    }
);

print "---- set\n";
my %c = map { $_ => "${_}One"->new(n => 3) } qw/Moose Mouse/;
cmpthese(
    -1 => {
        map { my $y = $_; $_ => sub { $c{$y}->n(5) } }
        qw/Moose Mouse/
    }
);