Question

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 ); #this code not work

thanks.

Was it helpful?

Solution

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 :)

OTHER TIPS

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(); };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top