Usage Of Plugins Out Of CPAN
Hi, I understood how to use plugins which are already installed to CPAN. I install it and use. But how will I test my own plugins I wrote at local? For example, I want to test following code taken from dancer manual (https://metacpan.org/pod/Dancer2::Manual) # 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 ); }; 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" }; # every other day get '/' => sub { return "other day" }; start; # End of script When I rub test.pl I got following error: Can't locate Dancer2/Plugin/OnTuesday.pm in @INC Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
Hi, Now, it found Dancer2::Plugin::OnTuesday because I see different error message when I run test.pl: isa check for "options" failed: on_tuesday is not a HashRef! at (eval 118) line 200. eval {...} called at (eval 118) line 198 Dancer2::Core::Route::new("Dancer2::Core::Route", "method", "get", "options", "on_tuesday", "code", CODE(0x2785ad0), "regexp", "/", ...) called at /usr/local/share/perl/5.20.1/Dancer2/Core/App.pm line 997 Dancer2::Core::App::add_route(Dancer2::Core::App=HASH(0x1d00170), "options", "on_tuesday", "code", CODE(0x2785ad0), "regexp", "/", "method", "get", ...) called at /usr/local/share/perl/5.20.1/Dancer2/Core/DSL.pm line 231 Dancer2::Core::DSL::_normalize_route(Dancer2::Core::DSL=HASH(0x21edfd8), ARRAY(0xc67058), "/", "on_tuesday", CODE(0x2785ad0)) called at /usr/local/share/perl/5.20.1/Dancer2/Core/DSL.pm line 199 Dancer2::Core::DSL::get(Dancer2::Core::DSL=HASH(0x21edfd8), "/", "on_tuesday", CODE(0x2785ad0)) called at /usr/local/share/perl/5.20.1/Dancer2/Core/Role/DSL.pm line 71 Dancer2::Core::Role::DSL::__ANON__("/", "on_tuesday", CODE(0x2785ad0)) called at test.pl line 6 On Mon, May 25, 2015 at 11:53 PM, Kadir Beyazlı <kadirbeyazli@gmail.com> wrote:
Hi,
I understood how to use plugins which are already installed to CPAN. I install it and use. But how will I test my own plugins I wrote at local?
For example, I want to test following code taken from dancer manual (https://metacpan.org/pod/Dancer2::Manual)
# 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 ); };
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" };
# every other day get '/' => sub { return "other day" };
start; # End of script
When I rub test.pl I got following error:
Can't locate Dancer2/Plugin/OnTuesday.pm in @INC
Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
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...
};
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 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
Hi Andrew, On Tue, May 26, 2015 at 4:54 PM, Andrew Beverley <andy@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... [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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
On Tue, 2015-05-26 at 22:09 +0300, Kadir Beyazlı wrote:
return $route_sub->( $dsl, @args )
is a coderef.
But it doesn't necessarily return a coderef, and certainly didn't when I ran the code myself.
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;
Great, thanks for posting this. If you get a chance to submit a PR to improve the documentation then that would be very welcome.
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.
I don't know for sure off the top of my head whether the example is wrong, but if it definitely doesn't work exactly as detailed in the example, then please submit a PR or bug report. Thanks, Andy
"Andrew" == Andrew Beverley <andy@andybev.com> writes:
Andrew> On Tue, 2015-05-26 at 22:09 +0300, Kadir Beyazlı wrote:
return $route_sub->( $dsl, @args )
is a coderef.
Andrew> But it doesn't necessarily return a coderef, and certainly didn't when I Andrew> ran the code myself.
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;
So how the heck does this app know to use Dancer2::Plugin::Tuesday in the first place? It's totally not obvious. And where did MyDancer2::Plugin::Logout come from? This example doesn't seem to be all that useful because it's not showing and explaining how things work. We're not all web gods who do this day in and day out for $WORK... so of us are poor mortals with colds who feel fairly dumb right now. *grin* Should test.pl really be: use Dancer2; use lib "lib"; use MyDancer2::Plugin::Tuesday; get '/' => sub { tuesday; }; start; Instead? What am I missing? John
On Wed, 2015-05-27 at 20:09 -0400, John Stoffel wrote:
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;
So how the heck does this app know to use Dancer2::Plugin::Tuesday in the first place? It's totally not obvious. And where did MyDancer2::Plugin::Logout come from?
I think the code is a mix between the example in the manual and the changes that Kadir has made to get it to work for himself.
This example doesn't seem to be all that useful because it's not showing and explaining how things work.
The original example is here: https://metacpan.org/pod/Dancer2::Manual#Route-Decorators
We're not all web gods who do this day in and day out for $WORK... so of us are poor mortals with colds who feel fairly dumb right now.
Is the original example in the manual any better? If not, let us know what you think is missing, and I'm sure someone will add it (possibly me!)
Should test.pl really be:
use Dancer2; use lib "lib"; use MyDancer2::Plugin::Tuesday;
get '/' => sub { tuesday; }; start;
Instead?
The one in the Kadir's email, probably yes, but that's different to the one in the actual manual. Andy
Hi John and Andrew, On Thu, May 28, 2015 at 3:09 AM, John Stoffel <john@stoffel.org> wrote:
"Andrew" == Andrew Beverley <andy@andybev.com> writes:
Andrew> On Tue, 2015-05-26 at 22:09 +0300, Kadir Beyazlı wrote:
return $route_sub->( $dsl, @args )
is a coderef.
Andrew> But it doesn't necessarily return a coderef, and certainly didn't when I Andrew> ran the code myself.
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;
So how the heck does this app know to use Dancer2::Plugin::Tuesday in the first place? It's totally not obvious. And where did MyDancer2::Plugin::Logout come from?
This example doesn't seem to be all that useful because it's not showing and explaining how things work. We're not all web gods who do this day in and day out for $WORK... so of us are poor mortals with colds who feel fairly dumb right now. *grin*
Should test.pl really be: Yes it should be like this. Instead of word Tuesday I used Logout by mistake
use Dancer2; use lib "lib"; use MyDancer2::Plugin::Tuesday;
get '/' => sub { tuesday; }; start;
Instead? What am I missing?
The first code was taken from following link and it does not work, I got error , I tried again https://metacpan.org/pod/Dancer2::Manual#Writing-a-plugin Then I found another link for plugins: https://metacpan.org/pod/Dancer2::Plugins At this link the example was simpler, and was about Logout. I applied it to Tuesday example and while copying here I made a mistake Regarding first code, I got error but perhaps I am using wrongly so it is better to offer change it after being sure
John _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
participants (3)
-
Andrew Beverley -
John Stoffel -
Kadir Beyazlı