a couple of questions re. Plugins
1. Does a plugin have to reside under Dancer::Plugin::MyPlugin? Or, can it be situated anywhere, like, for example, under app/lib? 2. Is it possible to call a method in the application from within MyPlugin? Here is what I mean -- In my app.pm, I have a method called sub opts { } I typically set a bunch of template options in the above sub routine, and use it like so get '/' => sub { my $opts = opts(); template 'index', $opts; }; I am creating plugin that automatically generates a web page like so return engine('template')->apply_layout($content); I would like to run `app::opts()`, and pass the $opts to the template from within the plugin. I am at a loss how to do that. -- Puneet Kishor
bump... anyone, any thoughts? On Nov 13, 2011, at 4:54 PM, Puneet Kishor wrote:
1. Does a plugin have to reside under Dancer::Plugin::MyPlugin? Or, can it be situated anywhere, like, for example, under app/lib?
2. Is it possible to call a method in the application from within MyPlugin? Here is what I mean --
In my app.pm, I have a method called
sub opts { }
I typically set a bunch of template options in the above sub routine, and use it like so
get '/' => sub { my $opts = opts(); template 'index', $opts; };
I am creating plugin that automatically generates a web page like so
return engine('template')->apply_layout($content);
I would like to run `app::opts()`, and pass the $opts to the template from within the plugin. I am at a loss how to do that.
-- Puneet Kishor
On Tue, Nov 15, 2011 at 8:19 PM, Puneet Kishor <punk.kish@gmail.com> wrote:
bump... anyone, any thoughts?
My only thoughts (which I actually came up with a few times) are: "Why?" Why would you want to put stuff in some directory without installing it? We have local::lib, we have carton, we're past installation problems. We have some good solutions for that. Why would you want to create a module under a different namespace? You can have a dist with multiple modules in multiple namespaces. Put your Dancer::Plugin::MyCompanyLogic in your MyCompanyNamespace::WebApp. Why put a module under a completely different namespace? I sometimes don't understand the base reasoning that leads to the situations you're encountering, and it leaves me wondering why. Have a great day (or evening, depends where you are located :) S.
I had asked if a Dancer plugin could be placed in some other location besides Dancer::Plugin:: to which, sawyer replied with... On Nov 15, 2011, at 2:19 PM, sawyer x wrote:
On Tue, Nov 15, 2011 at 8:19 PM, Puneet Kishor <punk.kish@gmail.com> wrote:
bump... anyone, any thoughts?
My only thoughts (which I actually came up with a few times) are: "Why?"
Why would you want to put stuff in some directory without installing it? We have local::lib, we have carton, we're past installation problems. We have some good solutions for that.
fair question, so I should explain why I asked the above question in the first place. My question stems really from my own lack of experience with creating Perl modules that may potentially be used by others. Here is my workflow... I got an idea for a Dancer::Plugin::Meta (DPM) based on Dancer::Plugin::SiteMap (DPS). DPS is installed in /opt/local/lib/perl5/site_perl/5.14.1/Dancer/Plugin ("lib") so I went into "lib", made a copy of DPS, renamed it DPM, and hacked it to do what I wanted. Of course, continuing to refine DPM while under "lib" is a pain in the ass because I have `sudo` every time, and, besides, I don't want to break my existing apps while I further experiment with DPM. So, I made a copy of DPM in my own home directory. To share it with the rest of the world, I also made that home directory version into a git repo, and put it on github. So, now I have /opt/local/lib/perl5/site_perl/5.14.1/Dancer/Plugin/Meta.pm which is currently powering my apps ~/Projects/Dancer/Plugin/Meta.pm which is a git repo and where I continue to hack it I am not knowledgable about the workflow whereby I can make experimental changes to ~/Projects/Dancer/Plugin/Meta.pm and test it in the development versions of my apps, and when I am satisfied with the changes, update my site_perl version with the new version (Of course, pushing the changes to github is easy). Any guidance on this would be appreciated. -- Puneet Kishor
On Friday 02 December 2011 16:48:26 Puneet Kishor wrote:
Of course, continuing to refine DPM while under "lib" is a pain in the ass because I have `sudo` every time, and, besides, I don't want to break my existing apps while I further experiment with DPM. So, I made a copy of DPM in my own home directory. To share it with the rest of the world, I also made that home directory version into a git repo, and put it on github.
So, now I have
/opt/local/lib/perl5/site_perl/5.14.1/Dancer/Plugin/Meta.pm which is currently powering my apps ~/Projects/Dancer/Plugin/Meta.pm which is a git repo and where I continue to hack it
Firstly, I'd recommend adopting the usual layout of a CPAN module of a dir named Dancer-Plugin-Meta, containing lib/Dancer/Plugin/Meta.pm, along with a makefile, tests, etc. It's much easier to use e.g. Module::Starter to rock that up for you. Also, I'd recommend not manually editing/creating stuff under your site_perl directories - reserve that for modules actually installed via the CPAN toolchain.
I am not knowledgable about the workflow whereby I can make experimental changes to ~/Projects/Dancer/Plugin/Meta.pm and test it in the development versions of my apps, and when I am satisfied with the changes, update my site_perl version with the new version (Of course, pushing the changes to github is easy).
Any guidance on this would be appreciated.
You probably want to look at using local::lib - or simply start your app when you're developing with a -I argument to add a dir to @INC, e.g.: perl -I~/Projects/Dancer-Plugin-Meta/lib bin/app.pl Doing that will mean that your dev version of the plugin will be found first in @INC and loaded instead of the system-wide one. When you're happy with the changes in your dev version, you could install them as you would any other Perl module with e.g. "perl Makefile.PL; make install" if you have set up your module as a CPAN-ready package layout using e.g. Module::Starter as previously mentioned. Of course, you could just add a "use lib ..." statement to your app while you're working on it, but that's a little bodgier :) Hope that's of some help? -- David Precious ("bigpresh") http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
Thanks David, Your reply is a great help. On Dec 2, 2011, at 11:38 AM, David Precious wrote:
On Friday 02 December 2011 16:48:26 Puneet Kishor wrote:
Of course, continuing to refine DPM while under "lib" is a pain in the ass because I have `sudo` every time, and, besides, I don't want to break my existing apps while I further experiment with DPM. So, I made a copy of DPM in my own home directory. To share it with the rest of the world, I also made that home directory version into a git repo, and put it on github.
So, now I have
/opt/local/lib/perl5/site_perl/5.14.1/Dancer/Plugin/Meta.pm which is currently powering my apps ~/Projects/Dancer/Plugin/Meta.pm which is a git repo and where I continue to hack it
Firstly, I'd recommend adopting the usual layout of a CPAN module of a dir named Dancer-Plugin-Meta, containing lib/Dancer/Plugin/Meta.pm, along with a makefile, tests, etc. It's much easier to use e.g. Module::Starter to rock that up for you.
Heh, heh... you might be amused to note that I just downloaded D::P::Database and used that as a template. Your plugin was my Module::Starter.
Also, I'd recommend not manually editing/creating stuff under your site_perl directories - reserve that for modules actually installed via the CPAN toolchain.
Right... that is what I want to learn.
I am not knowledgable about the workflow whereby I can make experimental changes to ~/Projects/Dancer/Plugin/Meta.pm and test it in the development versions of my apps, and when I am satisfied with the changes, update my site_perl version with the new version (Of course, pushing the changes to github is easy).
Any guidance on this would be appreciated.
..
Of course, you could just add a "use lib ..." statement to your app while you're working on it, but that's a little bodgier :)
Actually, I just used `use lib` and while verbose, it works quite well. Now, my follow-up question is to confirm the sanity of the following workflow -- 1. ~/Projects/Dancer-Plugin-Meta/ which has the usual CPAN module layout and is also a git repo 2. github/punkish/Dancer-Plugin-Meta the git repo for the world 3. /opt/../site_perl/5.14.1/Dancer/Plugin/Meta where the production version will get installed 4. ~/Sites/app/lib/app.pm which has use lib ('~/Projects/Dancer-Plugin-Meta/lib'); use Dancer::Plugin::Meta; I hack on Meta.pm under #1 above, and check if it is working well in #4 above. Once all is copacetic, I `git push` #1 to update #2. And, I do a `perl Makefile.PL && make && sudo make install` in #1 to update #3. Once I send my plugin to CPAN, I can use other cpan tools do this final bit as well. Does that make sense? -- Puneet Kishor
On Sunday 13 November 2011 22:54:17 Puneet Kishor wrote:
1. Does a plugin have to reside under Dancer::Plugin::MyPlugin? Or, can it be situated anywhere, like, for example, under app/lib?
There's no reason the plugin couldn't be e.g. appdir/lib/Dancer/Plugin/MyPlugin.pm, if that's what you're asking. There's no absolute need for plugins to live under the Dancer::Plugin namespace, although it's probably a good idea - and also, the plugin_setting() method exposed by Dancer::Plugin to allow plugins to get their config might not work properly as I think it expects to remove Dancer::Plugin:: from the start of the package name. If you really want your plugin under a different namespace - try it and see :)
2. Is it possible to call a method in the application from within MyPlugin? Here is what I mean --
In my app.pm, I have a method called
sub opts { }
I typically set a bunch of template options in the above sub routine, and use it like so
get '/' => sub { my $opts = opts(); template 'index', $opts; };
I am creating plugin that automatically generates a web page like so
return engine('template')->apply_layout($content);
I would like to run `app::opts()`, and pass the $opts to the template from within the plugin. I am at a loss how to do that.
If you 'use Dancer' as well as 'use Dancer::Plugin' within your plugin, I think you should be able to call template() from within the plugin. Cheers Dave P -- David Precious ("bigpresh") http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
participants (3)
-
David Precious -
Puneet Kishor -
sawyer x