Plugin nesting in Dancer2
Hello all! I have a question about the plugin nesting in Dancer2. I'd like to know if using a plugin (notably the Dancer2::Plugin::Database) inside another plugin is doable or deprecated or hacky or unsupported or just can't work. This used to work with Dancer1, but now it's unclear if and how it should work, as we don't do "use Dancer qw/:syntax/" anymore. The app at runtime seems to work, I get the expected output of the dumped D2::P::D object, but in a Dancer2 qw/:script/ it fails with a "deep recursion" error. use Dancer2 ":script"; use Dancer2::Plugin::Test; print to_dumper(hello); perl -I lib t/script.t 1..1 DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /home/melmoth/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/Dancer2/Plugin/Database.pm line 57. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. Deep recursion on anonymous subroutine at /home/melmoth/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/Dancer2/Core/Role/DSL.pm line 72. # Looks like your test exited with 255 before it could output anything. The test app is the following (also packed in the tarball attached with the tests). conf: ### cut #### plugins: Database: driver: SQLite database: 'foo.sqlite' ### cut #### app: ### cut #### package MyApp; use strict; use warnings; use Dancer2; use Dancer2::Plugin::Test; our $VERSION = '0.1'; get '/' => sub { my $dbh = hello; return to_dumper($dbh); }; true; ### cut #### plugin: ### cut #### package Dancer2::Plugin::Test; use strict; use warnings; use Dancer2::Plugin; use Dancer2::Plugin::Database; register hello => sub { my $dsl = shift; return database; }; register_plugin; 1; ### cut #### Thanks in advance Best wishes -- Marco
As a follow up, I encountered the following problem when writing some D2 plugins: only the hooks of the last loaded plugin are kept, the others are lost. See the minimal example below. App: ## #!/usr/bin/env perl use FindBin; use lib "$FindBin::Bin/../lib"; use Dancer2; use Dancer2::Plugin::Second; use Dancer2::Plugin::First; our $VERSION = '0.1'; hook "first_after" => sub { debug "first after"; }; hook "first_before" => sub { debug "first before"; }; hook "second_after" => sub { debug "second after"; }; hook "second_before" => sub { debug "second before"; }; get '/' => sub { test; other_test; return "OK"; }; dance; ## First.pm package Dancer2::Plugin::First; use strict; use warnings; use Dancer2::Plugin; register_hook 'first_after'; register_hook 'first_before'; register test => sub { my $dsl = shift; execute_hook("first_before"); execute_hook("first_after"); }; register_plugin; 1; ## Second.pm package Dancer2::Plugin::Second; use strict; use warnings; use Dancer2::Plugin; register_hook 'second_after'; register_hook 'second_before'; register other_test => sub { my $dsl = shift; execute_hook("second_before"); execute_hook("second_after"); }; register_plugin; 1; ## end (also attached) When starting the app it dies: Invalid hook name `second_after' at /home/melmoth/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/Dancer2/Core/DSL.pm line 129. I inserted some debug into Core::DSL and this is the list of the hooks it's recognizing: $VAR1 = { 'before_error_render' => 'core.error.before', 'on_route_exception' => 'core.app.route_exception', 'after_error_render' => 'core.error.after', 'before_error_init' => 'core.error.init', 'after_serializer' => 'engine.serializer.after', 'first_before' => 'plugin.first.first_before', 'after' => 'core.app.after_request', 'init_error' => 'core.error.init', 'after_error' => 'core.error.after', 'before_error' => 'core.error.before', 'before_layout_render' => 'engine.template.before_layout_render', 'before' => 'core.app.before_request', 'after_request' => 'core.app.after_request', 'after_file_render' => 'handler.file.after_render', 'before_template_render' => 'engine.template.before_render', 'before_serializer' => 'engine.serializer.before', 'after_layout_render' => 'engine.template.after_layout_render', 'first_after' => 'plugin.first.first_after', 'before_file_render' => 'handler.file.before_render', 'before_request' => 'core.app.before_request', 'after_template_render' => 'engine.template.after_render' }; No trace of the second_after, second_before. If I invert the order of the loading, it dies on "first_after". Using only one of the two plugins appears fine. Any clue? Thanks in advance and best wishes -- Marco
We clearly haven't thought about this case. I really wish we could clean up the plugin completely. It still isn't what we want it to be. :( Could you please open an issue for this? On Wed, Oct 2, 2013 at 12:47 PM, Marco Pessotto <melmothx@gmail.com> wrote:
Hello all!
I have a question about the plugin nesting in Dancer2. I'd like to know if using a plugin (notably the Dancer2::Plugin::Database) inside another plugin is doable or deprecated or hacky or unsupported or just can't work.
This used to work with Dancer1, but now it's unclear if and how it should work, as we don't do "use Dancer qw/:syntax/" anymore.
The app at runtime seems to work, I get the expected output of the dumped D2::P::D object, but in a Dancer2 qw/:script/ it fails with a "deep recursion" error.
use Dancer2 ":script"; use Dancer2::Plugin::Test; print to_dumper(hello);
perl -I lib t/script.t 1..1 DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /home/melmoth/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/Dancer2/Plugin/Database.pm line 57. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. DEPRECATED: Dancer2::Plugin::Test calls 'dsl' instead of '$dsl->dsl'. at /tmp/MyApp/lib/Dancer2/Plugin/Test.pm line 6. Deep recursion on anonymous subroutine at /home/melmoth/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/Dancer2/Core/Role/DSL.pm line 72. # Looks like your test exited with 255 before it could output anything.
The test app is the following (also packed in the tarball attached with the tests).
conf: ### cut #### plugins: Database: driver: SQLite database: 'foo.sqlite' ### cut ####
app:
### cut ####
package MyApp; use strict; use warnings; use Dancer2; use Dancer2::Plugin::Test;
our $VERSION = '0.1';
get '/' => sub { my $dbh = hello; return to_dumper($dbh); };
true;
### cut ####
plugin:
### cut #### package Dancer2::Plugin::Test; use strict; use warnings;
use Dancer2::Plugin; use Dancer2::Plugin::Database;
register hello => sub { my $dsl = shift; return database; };
register_plugin; 1; ### cut ####
Thanks in advance
Best wishes
-- Marco
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (2)
-
Marco Pessotto -
sawyer x