Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ikebe (2247)

ikebe
  (email not shown publicly)
http://blog.shebang.jp/

Journal of ikebe (2247)

Friday November 17, 2006
12:21 AM

Where am I?

[ #31636 ]

Sledge based application does not know "Where am I?".
so, Sledge configurations are slightly verbose.

package MyApp::Config::_common;
use strict;
use vars qw(%C);
*Config = \%C;
 
$C{TMPL_PATH} = '/path/to/view';
# ...
 
1;

I should specify the fullpath of templates dir.

this is a typical directory structure of mine.

MyApp/ - HOME directory.
  lib/
    MyApp/
    MyApp/Pages.pm
  view/ - template files.
    index.tt
  htdocs/ - static files. (images, JavaScript, CSS)
    logo.gif
  etc/ - config files.

Catalyst like path_to impl.

package Sledge::Plugin::PathTo;
use strict;
use Path::Class;
 
sub import {
    my $pkg = caller(0);
    no strict 'refs';
    $pkg->mk_classdata(qw(home));
    $pkg->home(find_home($pkg));
    *{"$pkg\::path_to"} = sub {
        my ( $self, @path ) = @_;
        my $path = dir( $self->home, @path );
        if ( -d $path ) {
            return $path;
        }
        else {
            return file( $self->home, @path );
        }
    };
}
 
sub find_home {
    my $class = shift;
    (my $file = "$class.pm") =~ s{::}{/}g;
    if ( my $inc_entry = $INC{$file} ) {
        my $path = file($inc_entry)->absolute->cleanup->stringify;
        $path =~ s/$file$//;
        my $home = dir($path);
        $home = $home->parent while $home =~ /(b?lib|site_perl)$/;
        return $home->stringify if -d $home;
    }
}

yeah, I do not have to write the fullpath in config.

package MyApp::Pages;
use strict;
use Sledge::Pages::Compat;
use Sledge::Plugin::PathTo;
 
sub create_config {
    my $self = shift;
    my $config = MyApp::Config->instance;
    $config->{tmpl_path} = $self->path_to('view');
    $config;
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.