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> &nbsp; <br>$C{TMPL_PATH} = '/path/to/view';<br>#<nobr> <wbr></nobr>...<br> &nbsp; <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>&nbsp; lib/<br>&nbsp; &nbsp; MyApp/<br>&nbsp; &nbsp; MyApp/Pages.pm<br>&nbsp; view/ - template files.<br>&nbsp; &nbsp; index.tt<br>&nbsp; htdocs/ - static files. (images, JavaScript, CSS)<br>&nbsp; &nbsp; logo.gif<br>&nbsp; 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> &nbsp; <br>sub import {<br>&nbsp; &nbsp; my $pkg = caller(0);<br>&nbsp; &nbsp; no strict 'refs';<br>&nbsp; &nbsp; $pkg-&gt;mk_classdata(qw(home));<br>&nbsp; &nbsp; $pkg-&gt;home(find_home($pkg));<br>&nbsp; &nbsp; *{"$pkg\::path_to"} = sub {<br>&nbsp; &nbsp; &nbsp; &nbsp; my ( $self, @path ) = @_;<br>&nbsp; &nbsp; &nbsp; &nbsp; my $path = dir( $self-&gt;home, @path );<br>&nbsp; &nbsp; &nbsp; &nbsp; if ( -d $path ) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $path;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return file( $self-&gt;home, @path );<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; };<br>}<br> &nbsp; <br>sub find_home {<br>&nbsp; &nbsp; my $class = shift;<br>&nbsp; &nbsp; (my $file = "$class.pm") =~ s{::}{/}g;<br>&nbsp; &nbsp; if ( my $inc_entry = $INC{$file} ) {<br>&nbsp; &nbsp; &nbsp; &nbsp; my $path = file($inc_entry)-&gt;absolute-&gt;cleanup-&gt;stringify;<br>&nbsp; &nbsp; &nbsp; &nbsp; $path =~ s/$file$//;<br>&nbsp; &nbsp; &nbsp; &nbsp; my $home = dir($path);<br>&nbsp; &nbsp; &nbsp; &nbsp; $home = $home-&gt;parent while $home =~<nobr> <wbr></nobr>/(b?lib|site_perl)$/;<br>&nbsp; &nbsp; &nbsp; &nbsp; return $home-&gt;stringify if -d $home;<br>&nbsp; &nbsp; }<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> &nbsp; <br>sub create_config {<br>&nbsp; &nbsp; my $self = shift;<br>&nbsp; &nbsp; my $config = MyApp::Config-&gt;instance;<br>&nbsp; &nbsp; $config-&gt;{tmpl_path} = $self-&gt;path_to('view');<br>&nbsp; &nbsp; $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>&nbsp; &nbsp; my $class = shift;<br>&nbsp; &nbsp; my $pkg = caller;<br>&nbsp; &nbsp; no strict 'refs';<br>&nbsp; &nbsp; *{"$pkg\::foo"} = sub {<br>&nbsp; &nbsp; &nbsp; &nbsp; # do something<br>&nbsp; &nbsp; };<br>&nbsp; &nbsp; $pkg-&gt;register_hook(<br>&nbsp; &nbsp; &nbsp; &nbsp; AFTER_DISPATCH =&gt; sub {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # do something.<br>&nbsp; &nbsp; &nbsp; &nbsp; },<br>&nbsp; &nbsp; );<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> &nbsp; <br>use strict;<br>use base qw(Sledge::Plugin);<br>__PACKAGE__-&gt;add_methods(<br>&nbsp; &nbsp; plugin_method =&gt; sub {<br>&nbsp; &nbsp; &nbsp; &nbsp; my $self = shift;<br>&nbsp; &nbsp; &nbsp; &nbsp; #<nobr> <wbr></nobr>...<br>&nbsp; &nbsp; },<br>);<br> &nbsp; <br>__PACKAGE__-&gt;register_hooks(<br>&nbsp; &nbsp; AFTER_DISPATCH =&gt; sub {<br>&nbsp; &nbsp; &nbsp; &nbsp; my $self = shift;<br>&nbsp; &nbsp; &nbsp; &nbsp; #<nobr> <wbr></nobr>...<br>&nbsp; &nbsp; },<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> &nbsp; <br>sub dispatch_index {<br>&nbsp; &nbsp; my $self = shift;<br>&nbsp; &nbsp; # You can call the method which provided by plugins.<br>&nbsp; &nbsp; $self-&gt;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> &nbsp; <br>__PACKAGE__-&gt;setup;</tt></p></div> </blockquote><p>mod_perl handler.</p><blockquote><div><p> <tt>&lt;Location<nobr> <wbr></nobr>/&gt;<br>&nbsp; &nbsp; SetHandler perl-script<br>&nbsp; &nbsp; PerlHandler MyApp<br>&lt;/Location&gt;</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> &nbsp; <br>MyApp-&gt;run;</tt></p></div> </blockquote><p>TODO:<br>* handle extra path as arguments. (like Catalyst)</p><p>/item/detail/23 -&gt; MyApp::Pages::Item-&gt;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