我通常可以通过随机尝试这两个选项的不同排列来获得我想要的行为,但我仍然不能说我确切地知道它们的作用。有没有具体的例子来证明这种差异?
如果至少剩下 N 个参数,则:CaptureArgs(N)
匹配项。它用于非终端链式处理程序。
:Args(N)
仅在正好剩下 N 个参数时才匹配。
例如
sub catalog : Chained : CaptureArgs(1) {
my ( $self, $c, $arg ) = @_;
...
}
sub item : Chained('catalog') : Args(2) {
my ( $self, $c, $arg1, $arg2 ) = @_;
...
}
比赛
/catalog/*/item/*/*
CaptureArgs
用于Catalyst 中的链式方法。
Args
标志着链式方法的结束。
例如:
sub base_method : Chained('/') :PathPart("account") :CaptureArgs(0)
{
}
sub after_base : Chained('base_method') :PathPart("org") :CaptureArgs(2)
{
}
sub base_end : Chained('after_base') :PathPart("edit") :Args(1)
{
}
上面链接的方法匹配/account/org/*/*/edit/*
.
这里base_end
是链的结束方法。为了标记链接操作的结束,使用了Args
。如果使用CaptureArgs
,则意味着链仍在继续。
Args
也用于催化剂的其他方法中,用于指定方法的参数。
同样来自cpan Catalyst::D ispatchType::Chained:
The endpoint of the chain specifies how many arguments it
gets through the Args attribute. :Args(0) would be none at all,
:Args without an integer would be unlimited. The path parts that
aren't endpoints are using CaptureArgs to specify how many parameters
they expect to receive.