[dancer-users] Executing Hooks in dancer2

Rubén Amórtegui ruben.amortegui at gmail.com
Thu Sep 17 15:29:55 BST 2015


Hello,

I'm having some issues with the hooks, because it seems that runs more than
one time each time that I'm calling from my Dancer2 application.

I have this plugin:

--
package Dancer2::Plugin::MyPluginTest;
use Dancer2::Plugin;

register_hook 'my_new_hook';

register new_function => sub {
 execute_hook 'my_new_hook';
};

register_plugin;
--

And I have this Dancer test application:

--
use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;
{
    package MyTestApp;
    use Dancer2;
    use Dancer2::Plugin::MyPluginTest;

    my $var = 0;

    get '/' => sub {
      new_function;
      $var;
    };

    hook 'my_new_hook' => sub {
      $var++;
      debug "Executing hook -  $var";
    };
};

my $test = Plack::Test->create( MyTestApp->to_app );
my $res  = $test->request( GET '/' );
is( $res->content, '1', 'Var is 1 after one call on  /' );
done_testing();
--


When I run the test, the hook it's executed twice.

--
[MyTestApp:6554] debug @2015-09-17 08:26:33> Executing hook -  1 in
plugin.pl l. 21
[MyTestApp:6554] debug @2015-09-17 08:26:33> Executing hook -  2 in
plugin.pl l. 21

not ok 1 - Var is 1 after one call on  /
1..1

#   Failed test 'Var is 1 after one call on  /'
#   at plugin.pl line 29.
#          got: '2'
#     expected: '1'
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests
--

I just isolate this example of a plugin that I'm working on, but it had the
same problem (but execute the hooks three times).

Any advice?

Thanks in advance.

-- 
____________________________
Rubén Darío Amórtegui Medina
MCE - Ingeniero de Sistemas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150917/0b32b927/attachment.html>


More information about the dancer-users mailing list