小编j1n*_*3l0的帖子

如何避免 Type::Tiny::_build_coercion 中的“深度递归”错误/警告

我继承了以下针对Type-Tiny-1.004004编写的代码:

package Company::Types;

use Type::Library -base, -declare => qw< TruncatedString >;
use Type::Utils -all;

declare TruncatedString, as Str,
    where { length $_ },
    inline_as {
        my ($constraint, $varname) = @_;
        
        return sprintf ( '!defined %s or %s',
            $varname,
            $constraint->parent->inline_check($varname),
        );
    },
    constraint_generator => sub {
        my ($max) = @_;
        die q{Max length must be positive!} unless int($max) > 0;
        return sub { 
            (length $_) <= $max;
        };
    },
    inline_generator => sub {
        my ($max) = @_;
        return sub …
Run Code Online (Sandbox Code Playgroud)

perl coerce type-tiny

5
推荐指数
1
解决办法
92
查看次数

我们如何使用 Test2::V0 测试可选的哈希字段

我正在尝试研究如何使用Test2::V0测试可选的哈希字段。我目前有以下几点:

use 5.016;
use Test2::V0;

subtest 'optional fields in a hash' => sub {
    my $check = hash {
        field foo => qr/^[0-9]+$/;
        field bar => qr/^[a-zA-Z]+$/; # this field is optional
    };

    like(
        { foo => 1 },
        $check,
        'should pass when optional field is omitted',
    );

    like(
        { foo => 2, bar => 'a' },
        $check,
        'should pass when optional field is provided',
    );
};

done_testing;
Run Code Online (Sandbox Code Playgroud)

现在,如果我放弃对可选字段的检查:

my $check = hash {
    field foo => qr/^[0-9]+$/; …
Run Code Online (Sandbox Code Playgroud)

testing perl

4
推荐指数
1
解决办法
90
查看次数

标签 统计

perl ×2

coerce ×1

testing ×1

type-tiny ×1