开发者

How to delegate calling parameter ($self) to other method

I am learning mojolicious::lite.

In router, delegate the parameter to controller, use this code ok:

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

Should there any short hand method, eg:

get '/hello/:name' => ControllerTest::hello( shift 开发者_如何学C); #this code not work

thanks.


Disclaimer: I'm not a mojolicious hacker :)

That won't work since 'shift' pulls data from the current context (from @_). I would guess the shortest (short hand) would be:

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

or maybe by using a sub reference:

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

Then the first argument passed into hello would be all the args passed to the anonymous sub used. I haven't tried this but I suspect it will work :)


I think you should be able to call it as a method directly by using the fully qualified name, e.g.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜