File: nonvoid.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 (29 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (7)
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
use Contextual::Return;
use Test::More 'no_plan';
use Carp;

sub foo {
    return
        NONVOID { 4.2, 9.9 }
        VOID    { die 'Useless use of foo() in void context' }
    ;
}

# and later...

$foo = foo();
ok $foo                            => 'BOOLEAN context';

is 0+$foo, 9.9                     => 'NUMERIC context';

is "$foo", 9.9                     => 'STRING context';

is join(q{,}, foo()), '4.2,9.9'    => 'LIST context';

my $res = eval{ ;foo(); 1; };
my $exception = $@;

ok !$res                           => 'VOID context fails';

like $exception, qr/\QUseless use of foo() in void context/
                                   => 'Error msg correct';