8 has bar => ( is => "rw" );
9 has baz => ( is => "rw" );
20 has bar => ( is => "rw" );
21 has baz => ( is => "rw" );
24 my ( $class, @args ) = @_;
25 unshift @args, "bar" if @args % 2 == 1;
26 return $class->SUPER::BUILDARGS(@args);
39 has bar => ( is => "rw" );
40 has baz => ( is => "rw" );
42 around BUILDARGS => sub {
44 my ( $class, @args ) = @_;
46 unshift @args, "bar" if @args % 2 == 1;
48 return $class->$orig(@args);
57 foreach my $class (qw(Foo Bar Baz Biff)) {
58 is( $class->new->bar, undef, "no args" );
59 is( $class->new( bar => 42 )->bar, 42, "normal args" );
60 is( $class->new( 37 )->bar, 37, "single arg" );
62 my $o = $class->new(bar => 42, baz => 47);
63 is($o->bar, 42, '... got the right bar');
64 is($o->baz, 47, '... got the right baz');
67 my $o = $class->new(42, baz => 47);
68 is($o->bar, 42, '... got the right bar');
69 is($o->baz, 47, '... got the right baz');
73 foreach my $class (qw(Qux Quux)) {
74 my $o = $class->new(bar => 42, baz => 47);
75 is($o->bar, 42, '... got the right bar');
76 is($o->baz, 47, '... got the right baz');
81 like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
82 "new() requires a list or a HASH ref"
86 $class->new( [ 37 ] );
88 like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
89 "new() requires a list or a HASH ref"
93 $class->new( bar => 42, baz => 47, 'quux' );
95 like( $@, qr/You passed an odd number of arguments/,
96 "new() requires a list or a HASH ref"