idiom for programming the Perl OO way with Dancer
The following would hopefully be a welcome change from my usual "I can't deploy" emails ;-). I am looking for the right idiom(s) for programming correctly with Dancer. Right now, my dance.pm looks a mess package dance; use Dancer ':syntax'; # bunch of modules ##################### use Tie::File; use Tie::File::AsHash; use URI::Escape; use lib '/Users/punkish/Library/Perl'; use Markdown 'markdown'; my $markdown_opts = {url_prefix => '/', url_pattern => '-'}; # end bunch of modules ################# our $VERSION = '0.1'; get '/*?' => sub { my ($p) = splat; my $page_name = $p ? $p : config->{default_page}; if ( $page_name =~ /slideshow/ ) { template 'album.tt', {..}; } elsif ($page_name =~ /maps/ { template 'map.tt', {..}; } elsif ($page_name =~ /something_else/ { template 'something_else.tt', {..}; } else { template 'default.tt', {..}; } }; sub slideshow { .. } sub maps { .. } sub something_else { .. } The above is just the beginning. All those subs and methods not only make for a very long dance.pm, they also clash syntactically with Dancer's magic "get '/' => sub {}" idiom. Now, in the world of CGI::Application, I would actually sub-class CGI::Application, add my custom methods and junk to it, and then sub-class that custom instance into my application instance which would then be clean. Yes, I know, I am sweeping the crap under the carpet, but at least the carpet it clean. Is there something like that I can do with Dancer? Can I create a sub-class of Dancer, add my custom stuff to it, and then create application instances with it and program with it the Perl OO way? Has someone written a tute on that? :-) -- Puneet Kishor http://www.punkish.org Carbon Model http://carbonmodel.org Charter Member, Open Source Geospatial Foundation http://www.osgeo.org Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor Nelson Institute, UW-Madison http://www.nelson.wisc.edu ----------------------------------------------------------------------- Assertions are politics; backing up assertions with evidence is science =======================================================================
On Tue, 3 Aug 2010 09:39:26 -0500, P Kishor <punk.kish@gmail.com> wrote: [...]
Is there something like that I can do with Dancer? Can I create a sub-class of Dancer, add my custom stuff to it, and then create application instances with it and program with it the Perl OO way?
This will be possible when the refactoring I'm working on is done. You'll then be able to do multiple "Dancer::App" instances and mount them as you like. For instance: # lib/YourApp/Slideshow.pm package YourApp::Slideshow; use Dancer 'syntax'; get '/' => sub { "the root of the slideshow app" }; # you can have as many as you like # then in your starter script, like YourApp.pm package YourApp; use Dancer ':syntax'; load_app 'YourApp::Slideshow', prefix => '/slideshow'; dance; It's not OO, I know, but that's the way Dancer goes: a DSL way. I hope that fits your needs. PS: I'm actively working on that refactoring branch all the week, I hope it will be finished before our Hackaton schedulded for the 14th of august... Regards,
participants (2)
-
P Kishor -
sukria