File: 39-reentrant.t

package info (click to toggle)
libparams-validate-perl 1.31-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,360 kB
  • sloc: perl: 2,374; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 798 bytes parent folder | download | duplicates (5)
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
use strict;
use warnings;

use Params::Validate qw( validate SCALAR );
use Test::More;

for my $i ( 1 .. 1000 ) {
    ok( bar(), 'bar()' );
    is( foo( foo => $i ), $i, "reentrant validation works ($i)" );
}

done_testing();

sub foo {
    my %p = validate(
        @_,
        {
            foo => {
                callbacks => {
                    'call bar' => sub { bar() }
                },
            },
        },
    );

    return $p{foo};
}

sub bar {
    my %p = baz( baz => 42 );

    return $p{baz} == 42;
}

sub baz {
    my %p = validate(
        @_,
        {
            baz => {
                type      => SCALAR,
                callbacks => {
                    'is num' => sub { $_[0] =~ /^\d+$/ },
                },
            },
        },
    );

    return %p;
}