如何将调用参数($ self)委派给其他方法

Wei*_*yan 3 perl mojo mojolicious

我正在学习mojolicious :: lite。

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

get '/hello/:name' => sub {
  my $self = shift;
  ControllerTest::hello($self);
  };
Run Code Online (Sandbox Code Playgroud)

是否应该有任何简便的方法,例如:

get '/hello/:name' => ControllerTest::hello( shift ); #this code not work
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 5

免责声明:我不是恶意的黑客:)

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

get '/hello/:name' => sub { ControllerTest::hello( shift ); };
Run Code Online (Sandbox Code Playgroud)

或通过使用子引用:

get '/hello/:name' => \&ControllerTest::hello
Run Code Online (Sandbox Code Playgroud)

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