perldancer

Plugin DSL method 'request' is deprecated. Use '$self->app->request' instead'


In perl, I am using Dancer 2 frame work, and using this plugin

use Dancer2::Plugin::Deferred;
use Dancer2::Plugin::Locale::Wolowitz;

For statements like below:

my $method = request->method();

my $params = request->params;

I am getting following warnings on console:

Plugin DSL method 'request' is deprecated. Use '$self->app->request' instead'.

Please give your recommendations to solve it out, I am not sure which of these two modules are causing this.

thanks


Solution

  • Dancer2 is mostly object-oriented. It always passes a $self to your route handlers. The warning is pretty clear. You should not use that DSL keyword, but instead access the request via $self->app.

    You need to grab $self from the argument list of your route handlers. It doesn't matter whether you use an anonymous sub or a reference to a named sub, in the same package or any other package.

    use Dancer2;
    
    get '/' => \&main::foo;
    
    sub foo {
      my $self = shift;
      return $self->app->request->params;
    }