3

我正在学习 mojolicious::lite。

在路由器中,将参数委托给控制器,使用以下代码即可:

get '/hello/:name' => sub {
  my $self = shift;
  ControllerTest::hello($self);
  };

是否有任何速记方法,例如:

get '/hello/:name' => ControllerTest::hello( shift ); #this code not work

谢谢。

4

2 回答 2

5

免责声明:我不是一个快乐的黑客:)

这行不通,因为 'shift' 从当前上下文(来自@_)中提取数据。我猜最短的(短手)是:

get '/hello/:name' => sub { ControllerTest::hello( shift ); };

或者也许通过使用子参考:

get '/hello/:name' => \&ControllerTest::hello

然后传入的第一个参数hello将是传递给使用的匿名子的所有参数。我还没有尝试过,但我怀疑它会起作用:)

于 2011-06-15T14:24:17.390 回答
1

我认为您应该可以使用完全限定名称直接将其作为方法调用,例如

get '/hello/:name' => sub { $self->ControllerTest::hello(); };
于 2011-06-15T15:37:06.777 回答