[dancer-users] Usage Of Plugins Out Of CPAN

Kadir Beyazlı kadirbeyazli at gmail.com
Tue May 26 20:09:19 BST 2015


Hi Andrew,


On Tue, May 26, 2015 at 4:54 PM, Andrew Beverley <andy at andybev.com> wrote:
> On Mon, 2015-05-25 at 23:53 +0300, Kadir Beyazlı wrote:
>> Hi,
>
> Hi Kadir, I can see 2 problems with your code:
>
>> # Start of plugin
>> package Dancer2::Plugin::OnTuesday;
>> # ABSTRACT: Make sure a route only works on Tuesday
>> use Dancer2::Plugin;
>>
>> register on_tuesday => sub {
>>     my ( $dsl, $route_sub, @args ) = plugin_args(@_);
>>
>>     my $day = (localtime)[6];
>>     $day == 2 or return pass;
>>
>>     return $route_sub->( $dsl, @args );
>
>                   ^^^^
> If you want to use your keyword as function on your route (as you are
> doing), then you need to return a code ref here. See this for an
> example:
>
> https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible/blob/master/lib/Dancer2/Plugin/Auth/Extensible.pm#L238
>[KB] I got above example from following link: https://metacpan.org/pod/Dancer2::Manual. I think the code below

return $route_sub->( $dsl, @args )

is a coderef. But I read another article from following link :
search.cpan.org/dist/Dancer2/lib/Dancer2/Plugin.pm

and saw that there are simpler ways of writing and calling a plugin.
In my opinion, the example at first link is not good because people
starting to Dancer2 read this article. I did following and it worked:

  package Dancer2::Plugin::Tuesday;
  use Dancer2::Plugin;

  register tuesday => sub {
    my $dsl  = shift;
    my $app  = $dsl->app;
    my $conf = plugin_setting();

    return "today is tuesday";
  };

  register_plugin;
  1;

and called in a simpler way  :

#test.pl
use Dancer2;
use lib "lib";

use MyDancer2::Plugin::Logout;

get '/' => sub { tuesday };

start;

It wrotes "today is tuesday" which means that I could call keyword 'on_tuesday'
>> };
>>
>> register_plugin;
>> # End of plugin
>>
>> To be able to use above plugin at my local, I created .pm file at
>> following folder:
>>
>> MyApp/lib/Dancer2/Plugin/OnTuesday.pm
>>
>> I wrote following code at folder MyApp/test.pl
>>
>> # Start of script
>> use Dancer2;
>> use lib "lib";
>>
>> use Dancer2::Plugin::OnTuesday;
>>
>> get '/' => on_tuesday => sub { return "tuesday" };
>
>                        ^^
> Your other mistake is that you need to remove this fat comma
>
[KB] I saw above call from first link. I copied it. If it is wrong I
think it should be changed.

> P.S. Make sure you view this plain text email in a fixed-width font (not
> the default for Gmail?), otherwise you will probably be removing the
> wrong fat comma ;-)
>
> Andy
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users



-- 
Kadir Beyazlı
Computer Engineer
GSM : +90 535 821 50 00


More information about the dancer-users mailing list