简体   繁体   English

使用MooseX :: Params :: Validate传递条件参数

[英]Conditional parameter passing with MooseX::Params::Validate

I work alot with Moose packages in Perl that use MooseX::Params::Validate to define an interface. 我在Perl中使用Moose包很多,使用MooseX::Params::Validate来定义接口。 These interfaces tend to be rather flexible and allow for multiple optional parameters. 这些接口往往相当灵活,允许多个可选参数。 Unfortunately this is Perl so the return types will vary based on the optional parameters and there is a benefit to passing the optional parameters in most cases when defined in the caller. 不幸的是,这是Perl,因此返回类型将根据可选参数而变化,并且在调用者中定义的大多数情况下传递可选参数是有好处的。 Various methods exported from MooseX::Params::Validate are used in this codebase so due to the various ways that package will be handled undef parameters I can not passing it will be graceful in anyway. 从这个代码库中使用从MooseX::Params::Validate导出的各种方法,因此,由于处理包的各种方式, undef参数我无法通过它无论如何都会优雅。 I tend to use the following method, but it comes up a lot in reviews, and I would like to ask if there is another way to achieve this flexibility. 我倾向于使用以下方法,但它在评论中出现了很多,我想问是否有另一种方法来实现这种灵活性。

use strict;
use warnings;

my $bar;

Foo->foo({
    foo => 'I, Foo need a VERY flexible interface. ',
    $bar ? ( bar => $bar ) : ()
});

$bar = "Very flexible...";

Foo->foo({
    foo => 'I, Foo need a VERY flexible interface. ',
    $bar ? ( bar => $bar ) : ()
});

package Foo;

use Moose;
use MooseX::Params::Validate;

sub foo {
    my $self = shift;
    my ( $foo, $bar ) = validated_list(
      \@_,
      foo => { isa => 'Str' },
      bar => { isa => 'Str', optional => 1 },
    );

    print $foo . $bar . "\n";
}

1;

The ternary operator to check the variables defined state is what always leaves me wanting a // type parameter option, but I can't see anywhere this type of operation is supported. 检查变量defined状态的三元运算符总是让我想要一个//类型参数选项,但我看不到任何支持这种类型的操作的地方。

Answers for the caller is preferred as I don't want to (won't) change the interface of the various packages, but I am open to answers showing methods of handling undef parameters values being passed as well. 为解答caller是首选,因为我不希望(不会)改变各种封装的接口,但我愿意回答显示的处理方法undef传递值以及参数。

As Craig Estey said in his comment, I think it would make sense just to pass in the options to your function and not having to care whether they are undefined or not. 正如克雷格·埃斯蒂(Craig Estey)在评论中所说的那样,我认为仅仅将选项传递给你的函数并且不必关心它们是否未定义是有意义的。 Instead, strip undefined hash keys as a preprocessing step before calling MooseX::Params::Validate::validated_list() . 相反,在调用MooseX::Params::Validate::validated_list()之前,将未定义的散列键作为预处理步骤。 For example: 例如:

use strict;
use warnings;

my $bar;

Foo->foo({
    foo => 'I, Foo need a VERY flexible interface. ',
    bar => $bar
});

package Foo;

use Moose;
use MooseX::Params::Validate ();

sub foo {
    my $self = shift;

    my ( $foo, $bar ) = validated_list(
      \@_,
      foo => { isa => 'Str' },
      bar => { isa => 'Str', optional => 1 },
    );

    $bar //= 'undef';  # Just to avoid printing warning: Use of uninitialized value 
    print $foo . $bar . "\n";
}

sub validated_list {
    for (keys %{ $_[0]->[0] } ) {
        delete $_[0]->[0]{$_} if !defined $_[0]->[0]{$_};
    }
    return MooseX::Params::Validate::validated_list( @_ );
}
1;

The Maybe type might be what you are looking for. Maybe类型可能就是你要找的东西。 Normally MooseX::Params::Validate does not allow undef to be passed for things liked Str, forcing you to use a ternary to conditionally pass or not pass it. 通常MooseX :: PARAMS ::验证不允许 UNDEF传递的东西喜欢海峡,迫使你用一个三元有条件通过或不通过。 An optional variable will be undef anyways in the subroutine, but with Maybe it makes it easier for the caller to just pass through the variables they have (which may or not be defined). 在子例程中,可选变量无论如何都是undef,但是使用Maybe可以使调用者更容易通过他们拥有的变量(可以定义或不定义)。

sub foo {
    my $self = shift;
    my ( $foo, $bar ) = validated_list(
      \@_,
      foo => { isa => 'Str' },
      bar => { isa => 'Maybe[Str]', optional => 1 }
    );

    print $foo . $bar . "\n";
}

By using Maybe[Str], it will now accept the undef value and you can handle this undef how you see fit in your subroutine. 通过使用Maybe [Str],它现在将接受undef值,你可以处理这个undef你认为适合你的子程序。

Maybe[`a] accepts either `a or undef.

http://search.cpan.org/dist/Moose-2.0604/lib/Moose/Manual/Types.pod http://search.cpan.org/dist/Moose-2.0604/lib/Moose/Manual/Types.pod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM