File: type_constraints.pl

package info (click to toggle)
libmoose-perl 2.4000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,408 kB
  • sloc: perl: 21,275; ansic: 291; makefile: 10
file content (53 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw[cmpthese];

=pod

This benchmark compares the overhead of a
auto-created type constraint vs. none at
all vs. a custom-created type.

=cut

{
    package Foo;
    use Moose;
    use Moose::Util::TypeConstraints;

    has 'baz' => (is => 'rw');
    has 'bar' => (is => 'rw', isa => 'Foo');
}

{
    package Bar;

    sub new { bless {} => __PACKAGE__ }
    sub bar {
        my $self = shift;
        $self->{bar} = shift if @_;
        $self->{bar};
    }
}

my $foo = Foo->new;
my $bar = Bar->new;

cmpthese(200_000,
    {
        'hand coded' => sub {
            $bar->bar($bar);
        },
        'w/out_constraint' => sub {
            $foo->baz($foo);
        },
        'w_constraint' => sub {
            $foo->bar($foo);
        },
    }
);

1;