Hello fellow Dancer users, I've been programming perl quite a few years now but am starting a new job soon, they use Dancer so I thought I would have a bash at it before I start there. I've been having a play at making something and I'm loving Dancer but I seem to have hit on a problem, I'm not sure if it's a bug in Dancer (it almost never is I know!) or if I am misunderstanding how I should be using pass but here it is anyway: I'm using Module::Load to load a module (containing routes) at runtime and then calling pass. If there are no matching routes this causes a RuntimeError. Example: $ cat bin/app.pl #!/usr/bin/env perl use Dancer; use Demo; dance; __END__ $ cat lib/Demo.pm package Demo; use Dancer qw( :syntax !load ); use Module::Load; any '/Monitoring/*?' => sub { my $module = "Monitoring"; load $module; pass(); }; true; __END__ $ cat lib/Monitoring.pm package Monitoring; use Dancer ':syntax'; prefix '/Monitoring'; get '/' => sub { return 'Monitoring Service'; }; get '/ping' => sub { return time; }; true; __END__ Calling domain.com/Monitoring/ping results in a timestamp as you would expect Calling domain.com/Monitoring/ returns the string "Monitoring Service". Calling domain.com/Monitoring/foo results in a Runtime Error where I would have expected to receive a 404 error Runtime Error runtime error Can't call method "run" on an undefined value at /usr/share/perl5/Dancer/Route.pm line 212. /usr/share/perl5/Dancer/Route.pm around line 212 209 $response->pass(0); 210 if ($self->next) { 211 my $next_route = $self->find_next_matching_route($request); 212 return $next_route->run($request); 213 } 214 else { 215 Dancer::Logger::core('Last matching route passed!'); Stack main in -e l. 0 main in -e l. 0 Plack::Handler::Apache2 in /usr/share/perl5/Plack/Handler/Apache2.pm l. 91 Plack::Handler::Apache2 in /usr/share/perl5/Plack/Handler/Apache2.pm l. 66 Dancer::Handler in /usr/share/perl5/Dancer/Handler.pm l. 123 Dancer::Handler in /usr/share/perl5/Dancer/Handler.pm l. 76 Dancer::Handler in /usr/share/perl5/Dancer/Handler.pm l. 113 Try::Tiny in /usr/share/perl5/Try/Tiny.pm l. 100 Dancer::Exception in /usr/share/perl5/Dancer/Exception.pm l. 47 Dancer::Exception in /usr/share/perl5/Dancer/Exception.pm l. 35 Dancer::Handler in /usr/share/perl5/Dancer/Handler.pm l. 107 Dancer::Object in /usr/share/perl5/Dancer/Object.pm l. 16 Dancer::Error in /usr/share/perl5/Dancer/Error.pm l. 37 Dancer::Error in /usr/share/perl5/Dancer/Error.pm l. 276 I've not posted all of the RuntimeError since it contains sensitive information about my config file and server. If the rest is needed I can spend some time to run up a clean version without anything important in. I'm using the version of Dancer which comes with Debian 7. $ apt-cache show libdancer-perl Package: libdancer-perl Version: 1.3095+dfsg-1 I'm using apache2 with Plack::Handler::Apache2 to run the Dancer application. My real application is more complex than this but I knocked this one up so it would demo the problem with as little code as possible. Does anyone know why this RuntimeError occurs, and perhaps how I can avoid it in the situation where I need to load a module dynamically which contains routes. Alternatively should I report this on the Dancer github and then try and fix it? Thanks, Jonathan
On Sun, 29 Sep 2013, Jonathan Harden wrote:
I'm using Module::Load to load a module (containing routes) at runtime and then calling pass. If there are no matching routes this causes a RuntimeError.
I've never called pass (Don't know if it's a bug). But let me rewrite your example...
Example:
$ cat bin/app.pl #!/usr/bin/env perl use Dancer; use Demo; dance; __END__
So far so good.
$ cat lib/Demo.pm package Demo;
use Dancer qw(:syntax);
load_app 'Monitoring', prefix => '/my/monitoring/prefix';
true; __END__
$ cat lib/Monitoring.pm package Monitoring;
use Dancer ':syntax';
get '/' => sub { my $prefix = prefix; return "Monitoring Service running on $prefix; };
get '/ping' => sub { return time; };
true; __END__
Calling domain.com/Monitoring/foo results in a Runtime Error where I would have expected to receive a 404 error
That should give a 404 now. HTH Henk
participants (2)
-
Henk van Oers -
Jonathan Harden