fix punctuation
[gitmo/Moose.git] / benchmarks / type_constraints.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Benchmark qw[cmpthese];
7
8 =pod
9
10 This benchmark compares the overhead of a
11 auto-created type constraint vs. none at
12 all vs. a custom-created type.
13
14 =cut
15
16 {
17     package Foo;
18     use Moose;
19     use Moose::Util::TypeConstraints;
20
21     has 'baz' => (is => 'rw');
22     has 'bar' => (is => 'rw', isa => 'Foo');
23 }
24
25 {
26     package Bar;
27
28     sub new { bless {} => __PACKAGE__ }
29     sub bar {
30         my $self = shift;
31         $self->{bar} = shift if @_;
32         $self->{bar};
33     }
34 }
35
36 my $foo = Foo->new;
37 my $bar = Bar->new;
38
39 cmpthese(200_000,
40     {
41         'hand coded' => sub {
42             $bar->bar($bar);
43         },
44         'w/out_constraint' => sub {
45             $foo->baz($foo);
46         },
47         'w_constraint' => sub {
48             $foo->bar($foo);
49         },
50     }
51 );
52
53 1;