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
|
#!/usr/bin/perl
use Test;
BEGIN { plan tests => 9 }
use Class::MakeMethods::Composite::Universal;
ok( 1 );
########################################################################
sub foo {
my $count = shift || 0;
return 'foo' x $count;
}
ok( ! length foo() );
ok( foo(1) eq 'foo' );
ok( foo(2) eq 'foofoo' );
ok( foo(3) eq 'foofoofoo' );
Class::MakeMethods::Composite::Universal->make_patch(
name => 'foo',
pre_rules => [
sub {
my $method = pop @_;
if ( ! scalar @_ ) {
@{ $method->{args} } = ( 2 );
}
},
sub {
my $method = pop @_;
my $count = shift;
if ( $count > 99 ) {
Carp::confess "Won't foo '$count' -- that's too many!"
}
},
],
post_rules => [
sub {
my $method = pop @_;
if ( ref $method->{result} eq 'SCALAR' ) {
${ $method->{result} } =~ s/oof/oozle-f/g;
} elsif ( ref $method->{result} eq 'ARRAY' ) {
map { s/oof/oozle-f/g } @{ $method->{result} };
}
}
],
);
ok( foo(), foo(2) );
ok( foo(1), 'foo' );
ok( foo(2), 'foozle-foo' );
ok( foo(3), 'foozle-foozle-foo' );
1;
|