3

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.

Weiyan
  • 1,116
  • 3
  • 14
  • 25

2 Answers2

5

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

Lee
  • 311
  • 2
  • 4
  • I haven't tried it either, but this should work and is probably the shortest way. – Eric Strom Jun 15 '11 at 14:28
  • 1
    There's also the semi-mysterious (if you haven't read perlsub): `get '/hello/:name' => sub { &ControllerTest::hello; };` And if you want to get really weird: `get '/hello/:name' => sub { goto \&ControllerTest::hello; };` – daotoad Jun 15 '11 at 15:01
  • All methods in this answer works, include coment by daotoad. Thanks. – Weiyan Jun 16 '11 at 05:20
1

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(); };
friedo
  • 65,762
  • 16
  • 114
  • 184
  • Can't call method "param" on an undefined value at lib/ControllerTest.pm line 6. The parameter not pass to the hello() method. – Weiyan Jun 16 '11 at 05:14