qw; -6ren">
gpt4 book ai didi

perl - 如何避免 Type::Tiny::_build_coercion 中的 "deep recursion"错误/警告

转载 作者:行者123 更新时间:2023-12-03 16:32:17 25 4
gpt4 key购买 nike

我继承了以下针对 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 {
my ($constraint, $varname) = @_;

return sprintf(
'%s and length %s <= %d',
$constraint->parent->inline_check($varname),
$varname,
$max,
);
};
},
coercion_generator => sub {
my ($base, $derived, $max) = @_;
# $base - TruncatedString
# $derived - TruncatedString[<N>]
# $max - N

# Not sure if I should be adding the coercion to $base or $derived, but I suspect that
# $derived is the correct choice here.
$derived->coercion->add_type_coercions(Str,
sub {
return substr $_, 0, $max;
}
);
};
但是它不适用于 Type-Tiny-1.012000 。我编写了以下测试来说明问题:
use Test2::V0;
use Time::Out qw< timeout >;
use Company::Types qw< TruncatedString >;

subtest 'Coercing to TruncatedString->of(10)' => sub {
timeout 2 => sub { # test hangs without timeout
my $type = TruncatedString->of(10); # same as TruncatedString[10]
ok( try_ok { $type->coerce('123456789012') }, 'should not throw an exception' );
} || bail_out( 'Ran out of time: ' . $@ );
};

done_testing();
这将输出以下内容:
# Seeded srand with seed '20201120' from local date.
Deep recursion on subroutine "Type::Tiny::_build_coercion" at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 399.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 467.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 1015.
not ok 1 - Coercing to TruncatedString->of(10) {
not ok 1
# Failed test at t/truncated-string.t line 8.
# Exception: CODE(0x7fee0fb78998)
not ok 2 - should not throw an exception
# Failed test 'should not throw an exception'
# at t/truncated-string.t line 8.
1..2
}

# Failed test 'Coercing to TruncatedString->of(10)'
# at t/truncated-string.t line 10.
1..1
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests

Test Summary Report
-------------------
t/truncated-string.t (Wstat: 256 Tests: 1 Failed: 1)
Failed test: 1
Non-zero exit status: 1
Files=1, Tests=1, 4 wallclock secs ( 0.02 usr 0.01 sys + 3.43 cusr 0.38 csys = 3.84 CPU)
Result: FAIL
我已将问题缩小到 coercion_generator (如果我将其注释掉,我将丢失此错误)并在 docs 中注意到这一点:

The following attributes are used for parameterized coercions, but are not fully documented because they may change in the near future:


我认为这已经发生了变化,并希望得到一些关于如何更新我现有代码以迎头 catch 的帮助?

最佳答案

Not sure if I should be adding the coercion to $base or $derived


两者都不。你应该退货。
coercion_generator => sub {
my ($base, $derived, $max) = @_;
return Type::Coercion->new(
type_coercion_map => [ Str, sub { substr $_, 0, $max } ]
);
};
Parameterizable Types in Type::Tiny::Manual::Libraries — 它提供了一个很好的解释和示例,说明如何使用内联和强制来处理参数化类型。
我已经检查了当前 Type::Tiny 版本中的上述作品,甚至是 0.006(2013 年 5 月发布)的旧版本!您在原始代码中的使用方式从来都不是 coercion_generator 的预期用途。我有点惊讶它曾经奏效过。

此外,如果您对为什么会收到有关深度强制的警告感兴趣,这是由 $type->coercion 引起的。行为很像 Moo/Moose 中的惰性属性,它调用您的 coderef,但您的 coderef 调用 $type->coercion .

关于perl - 如何避免 Type::Tiny::_build_coercion 中的 "deep recursion"错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64935422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com