ikebe's Journal
http://use.perl.org/~ikebe/journal/
ikebe's use Perl Journal
en-us
use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners.
2012-01-25T02:38:11+00:00
pudge
pudge@perl.org
Technology
hourly
1
1970-01-01T00:00+00:00
ikebe's Journal
http://use.perl.org/images/topics/useperl.gif
http://use.perl.org/~ikebe/journal/
-
Where am I?
http://use.perl.org/~ikebe/journal/31636?from=rss
<p>Sledge based application does not know "Where am I?".<br>so, Sledge configurations are slightly verbose.</p><blockquote><div><p> <tt>package MyApp::Config::_common;<br>use strict;<br>use vars qw(%C);<br>*Config = \%C;<br>
<br>$C{TMPL_PATH} = '/path/to/view';<br>#<nobr> <wbr></nobr>...<br>
<br>1;</tt></p></div> </blockquote><p>I should specify the fullpath of templates dir.</p><p>this is a typical directory structure of mine.</p><blockquote><div><p> <tt>MyApp/ - HOME directory.<br> lib/<br> MyApp/<br> MyApp/Pages.pm<br> view/ - template files.<br> index.tt<br> htdocs/ - static files. (images, JavaScript, CSS)<br> logo.gif<br> etc/ - config files.</tt></p></div> </blockquote><p>Catalyst like path_to impl.</p><blockquote><div><p> <tt>package Sledge::Plugin::PathTo;<br>use strict;<br>use Path::Class;<br>
<br>sub import {<br> my $pkg = caller(0);<br> no strict 'refs';<br> $pkg->mk_classdata(qw(home));<br> $pkg->home(find_home($pkg));<br> *{"$pkg\::path_to"} = sub {<br> my ( $self, @path ) = @_;<br> my $path = dir( $self->home, @path );<br> if ( -d $path ) {<br> return $path;<br> }<br> else {<br> return file( $self->home, @path );<br> }<br> };<br>}<br>
<br>sub find_home {<br> my $class = shift;<br> (my $file = "$class.pm") =~ s{::}{/}g;<br> if ( my $inc_entry = $INC{$file} ) {<br> my $path = file($inc_entry)->absolute->cleanup->stringify;<br> $path =~ s/$file$//;<br> my $home = dir($path);<br> $home = $home->parent while $home =~<nobr> <wbr></nobr>/(b?lib|site_perl)$/;<br> return $home->stringify if -d $home;<br> }<br>}</tt></p></div> </blockquote><p>yeah, I do not have to write the fullpath in config.</p><blockquote><div><p> <tt>package MyApp::Pages;<br>use strict;<br>use Sledge::Pages::Compat;<br>use Sledge::Plugin::PathTo;<br>
<br>sub create_config {<br> my $self = shift;<br> my $config = MyApp::Config->instance;<br> $config->{tmpl_path} = $self->path_to('view');<br> $config;<br>}</tt></p></div> </blockquote>
ikebe
2006-11-17T05:21:28+00:00
journal
-
write Sledge plugins more easily.
http://use.perl.org/~ikebe/journal/31589?from=rss
<p>actually, Sledge does not have real plugin system..</p><p>currently, typical plugin codes are below.<br>write sub import {} and modify symbol tables directly.</p><blockquote><div><p> <tt>package Sledge::Plugin::Foo;<br>use strict;<br>sub import {<br> my $class = shift;<br> my $pkg = caller;<br> no strict 'refs';<br> *{"$pkg\::foo"} = sub {<br> # do something<br> };<br> $pkg->register_hook(<br> AFTER_DISPATCH => sub {<br> # do something.<br> },<br> );<br>}</tt></p></div> </blockquote><p>I think this style is slightly complex..<br>so, I propose some modules to write plugins easily.</p><p>use Sledge::Plugin as a baseclass.</p><blockquote><div><p> <tt>package Sledge::Plugin::Bar;<br>
<br>use strict;<br>use base qw(Sledge::Plugin);<br>__PACKAGE__->add_methods(<br> plugin_method => sub {<br> my $self = shift;<br> #<nobr> <wbr></nobr>...<br> },<br>);<br>
<br>__PACKAGE__->register_hooks(<br> AFTER_DISPATCH => sub {<br> my $self = shift;<br> #<nobr> <wbr></nobr>...<br> },<br>);</tt></p></div> </blockquote><p>in your Pages, use Sledge::PluginLoader.</p><blockquote><div><p> <tt>package MyApp::Pages::Root;<br>use strict;<br>use base qw(MyApp::Pages::Base);<br>use Sledge::PluginLoader qw(Bar); # load Sledge::Plugin::Bar.<br>
<br>sub dispatch_index {<br> my $self = shift;<br> # You can call the method which provided by plugins.<br> $self->plugin_method;<br>}</tt></p></div> </blockquote>
ikebe
2006-11-13T14:23:15+00:00
journal
-
Sledge::Engine
http://use.perl.org/~ikebe/journal/31469?from=rss
<p>I wrote the Sledge::Engine, the modern dispatcher of Sledge application.</p><p><a href="http://svn.shebang.jp/repos/Sledge-Engine/">http://svn.shebang.jp/repos/Sledge-Engine/</a></p><p>write application top-level module. MyApp.pm</p><blockquote><div><p> <tt>package MyApp;<br>use Sledge::Engine;<br>
<br>__PACKAGE__->setup;</tt></p></div> </blockquote><p>mod_perl handler.</p><blockquote><div><p> <tt><Location<nobr> <wbr></nobr>/><br> SetHandler perl-script<br> PerlHandler MyApp<br></Location></tt></p></div> </blockquote><p>CGI mode. write index.cgi.</p><blockquote><div><p> <tt>#!/usr/bin/perl<br>use strict;<br>use MyApp;<br>
<br>MyApp->run;</tt></p></div> </blockquote><p>TODO:<br>* handle extra path as arguments. (like Catalyst)</p><p>/item/detail/23 -> MyApp::Pages::Item->dispatch_detail with 1 argument '23'.<br>* custom mapping rules.<br>* mod_perl2 and FastCGI support.</p>
ikebe
2006-11-01T12:07:12+00:00
journal