File: method.t

package info (click to toggle)
libcontextual-return-perl 0.003001-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 324 kB
  • ctags: 52
  • sloc: perl: 1,187; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download | duplicates (6)
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
use Contextual::Return;
use Test::More 'no_plan';
use strict;

sub foo_with_default_method {
    return
        METHOD {
            bar           => sub { 'bar method called'     },
            qr/ba(.)/     => sub { $1 . ' method called'   },
            ['qux','dux'] => sub { "$_ method called"      },
            qr/.*/        => sub { 'DEFAULT method called' },
        }
        DEFAULT { 'DEFAULT value' }
}

is foo_with_default_method()->bar, 'bar method called',     'bar method';
is foo_with_default_method()->baz, 'z method called',       'baz method';
is foo_with_default_method()->qux, 'qux method called',     'qux method';
is foo_with_default_method()->dux, 'dux method called',     'dux method';
is foo_with_default_method()->jax, 'DEFAULT method called', 'DEFAULT method';
is foo_with_default_method()     , 'DEFAULT value',         'DEFAULT';


sub foo_with_method_and_obj {
    return
        METHOD {
            bar => sub { 'bar method called' },
        }
        OBJREF {
            bless {}, 'Bar';
        }
        DEFAULT { 'DEFAULT value' }
}

is foo_with_method_and_obj()->bar, 'bar method called',     'bar method called';
is foo_with_method_and_obj()->baz, 'Bar::baz',              'OBJREF method called';
is foo_with_method_and_obj()     , 'DEFAULT value',         'DEFAULT value';




package Bar;

sub baz { "Bar::baz" }