-1

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

I_G
  • 13
  • 4
  • I think you are using either the Dancer or Dancer2 framework, or the Mojolicious framework, but I cannot be sure without seeing your code. – simbabque Oct 26 '18 at 10:20
  • 1
    Thanks for welcome, I have edited my question, hopefully it makes sense now. If you are not getting it, please let me know, I may modify it and make it clearer. – I_G Oct 26 '18 at 10:39
  • Which version of Dancer2? Please run `perl -MDancer2\ 99` on the terminal, including the space after the backslash. – simbabque Oct 26 '18 at 10:48
  • I cannot reproduce on Dancer2 0.206000 and Dancer2::Plugin::Deferred 0.007017. Are you using something else as well? Please [edit] again and include a [mcve]. – simbabque Oct 26 '18 at 10:52
  • Its 0.206000 on running this `perl -MDancer2\ 99 ` I am getting following error: – I_G Oct 26 '18 at 10:53
  • `Dancer2 version 99 required--this is only version 0.206000 at /usr/local/share/perl5/Dancer2.pm line 17. BEGIN failed--compilation aborted.` – I_G Oct 26 '18 at 10:53
  • That error message was expected. This is the fastest way to get the version number. – simbabque Oct 26 '18 at 10:56
  • So we have the same D2 version, but I don't get the error. Please make a full program I can run. Anyway, the obvious solution is to do what it says in the warning and use `$self->app->request` instead. ;) – simbabque Oct 26 '18 at 10:57
  • Actually we are calling functions as ` get '/testModule/testSubModule/:test_id/update' => \&test_controller::testMethod; ` now in testMethod if we try to get `resuest->params; ` we are getting values but warnings too -;p,, – I_G Oct 26 '18 at 11:10
  • Please make a stand-alone program to demonstrate the warning. One route, as simple as possible. – simbabque Oct 26 '18 at 11:12
  • and we cant use $self there.. as if we call functions by using `::` – I_G Oct 26 '18 at 11:13
  • The colons have nothing to do with method calls. Dancer2 will always call route handlers as methods and pass along `$self`. That's just how it works. It doesn't matter if you provide a fresh anonymous sub or a reference to an existing sub by name to the `get` DSL helper. – simbabque Oct 26 '18 at 11:14
  • I am getting this error: `error @2018-10-29 01:33:10> Route exception: Can't call method "app" on an undefined value at /foo/foo_controller.pm line 227. in /usr/local/share/perl5/Dancer2/Core/App.pm l. 1473 ` – I_G Oct 29 '18 at 05:34
  • we are unable to get this `$self` object there. – I_G Oct 29 '18 at 05:35
  • its undef there: – I_G Oct 29 '18 at 05:37
  • ` $VAR1 = undef;` – I_G Oct 29 '18 at 05:37
  • 1
    You need to show the code you've used. Run my example. Does it work for you? Then apply the technique to your program. If mine works and yours doesn't you are doing something wrong. I can't help you if I can't see it. – simbabque Oct 29 '18 at 07:05

1 Answers1

2

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;
}
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • Thanks @simbabque It is working fine for **get** calls but we are getting error in ajax calls, we are calling this function as: `ajax '/_updateTest' => \&main::foo;` Error is : `error @2018-10-29 06:48:42> Route exception: Can't call method "app" on an undefined value at` – I_G Oct 29 '18 at 11:55
  • 1
    @I_G again, please provide a [mcve]. I can't help without knowing all the facts. Right now you are just wasting both our time if you don't tell me everything. Nothing is worse than guessing in IT. – simbabque Oct 29 '18 at 11:59
  • similar is the case for `ajax ['post'] => '/test/testCall' => \&main::foo;` – I_G Oct 29 '18 at 12:00
  • `use Dancer2; get '/' => \&main::foo; #working fine # $self object is undef for following type of calls ajax '/_updateTest' => \&main::foo; ajax ['post'] => '/test/testCall' => \&main::foo; sub foo { my $self = shift; return $self->app->request->params; }` – I_G Oct 29 '18 at 12:02
  • we have tested with your provided code, just changes the way of call, I mean ajax and post. @simbabque, you may please test the above code provided by you by updating the calling way at my end there is a routing file in which I change the calling methodology. – I_G Oct 29 '18 at 12:04
  • @I_G ok, NOW I can reproduce it. You should have said that you have the AJAX plugin from the start. The plugins you showed in the question are completely unrelated. – simbabque Oct 29 '18 at 12:04
  • @I_G indeed, the ::Ajax plugin doesn't allow this. I think that's a bug. https://metacpan.org/source/CROMEDOME/Dancer2-Plugin-Ajax-0.300000/lib/Dancer2/Plugin/Ajax.pm#L46. There is no evidence that this has been possible, and the last release of ::Ajax is over two years ago. I will file a bug report. – simbabque Oct 29 '18 at 12:07
  • @I_G I am currently filing a ticket on github. Feel free to grab it and supply a patch. The D2 maintainers are usually quick with accepting those, especially as we've still got a few days of Hacktoberfest left. – simbabque Oct 29 '18 at 12:21
  • I proposed a fix. – simbabque Oct 29 '18 at 12:41
  • @I_G my fix has been released, see https://metacpan.org/pod/Dancer2::Plugin::Ajax for version 0.400000. – simbabque Nov 13 '18 at 10:09
  • @I_G you're welcome. Please note I am not affiliated with the Dancer team, although I am friends with some of them. In the future, feel free to suggest such changes yourself. They always welcome the help! :) – simbabque Nov 14 '18 at 11:27
  • That's great! We are happy to find such response from Dancer 2. – I_G Nov 15 '18 at 12:06