File: interp.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 (56 lines) | stat: -rw-r--r-- 1,968 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
54
55
56
use Contextual::Return;
use Test::More 'no_plan';

my @todo_list = ( 'eat', 'drink', 'be merry' );

sub interp_explicit {
    return (
        SCALAR { scalar @todo_list }      # In scalar context: how many?
        LIST   { @todo_list        }      # In list context: what are they?

        SCALARREF { \scalar @todo_list }  # Scalar context value as ref
        ARRAYREF  { \@todo_list        }  # List context value as array ref
    );
}

sub interp_implicit {
    return (
        SCALAR { scalar @todo_list }      # In scalar context: how many?
        LIST   { @todo_list        }      # In list context: what are they?
    );
}

sub interp_num {
    return (
        NUM   { scalar @todo_list }      # In num context: how many?
        LIST  { @todo_list        }      # In list context: what are they?
    );
}

sub interp_str {
    return (
        NUM   { @todo_list + 1    }      # In num context: how many + 1?
        STR   { scalar @todo_list }      # In str context: how many?
        LIST  { @todo_list        }      # In list context: what are they?
    );
}


is "There are ${interp_explicit()} ToDo tasks: @{interp_explicit()}",
   'There are 3 ToDo tasks: eat drink be merry'
                                                => 'Explicit interpolators';

is "There are ${interp_implicit()} ToDo tasks: @{interp_implicit()}",
   'There are 3 ToDo tasks: eat drink be merry'
                                                => 'Implicit interpolators';

is "There are ${interp_num()} ToDo tasks: @{interp_num()}",
   'There are 3 ToDo tasks: eat drink be merry'
                                                => 'Numeric interpolators';

is "There are ${interp_str()} ToDo tasks: @{interp_str()}",
   'There are 3 ToDo tasks: eat drink be merry'
                                                => 'String interpolators';

is 0+${interp_str()}, "4"                   => 'Smart numbers';
is "".${interp_str()}, "3"                  => 'Smart strings';