File::ShareDir: A tutorial

File::ShareDir is a neat little module you can use to access your non-code files that you need to pack with your distribution. Files like images, Template::Toolkit templates, etc.

One cool use I've found for File::ShareDir is using it to replace the jumble of code that is currently being used for Catalyst::Helper's file generation. I was able to cut out the stuff from the DATA section, put it in a share/ directory in the distribution, run a make install and voila. I'm able to access my files, and even throw Path::Class into the mix to get easy file slurping and such.

Here's a bit of code I used:

sub get_sharedir_file {
     my ($self, @filename) = @_;
     my $file = file( dist_dir('Catalyst-Devel'), @filename);
     warn $file;
     my $contents = $file->slurp;
     return $contents;
 }

 sub render_file {
     my ( $self, $file, $path, $vars ) = @_;
     $vars ||= {};
     my $t = Template->new;
     my $template = $self->get_sharedir_file( 'root', $file );
     return 0 unless $template;
     my $output;
     $t->process( \$template, { %{$self}, %$vars }, \$output )
       || Catalyst::Exception->throw(
         message => qq/Couldn't process "$file", / . $t->error() );
     $self->mk_file( $path, $output );
 }

 sub _mk_test {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
     $self->render_file( 'test.tt', "$script/$appprefix\_test.pl" );
     chmod 0700, "$script/$appprefix\_test.pl";
 }

file() is a Path::Class method for returning a file object, with which you can call ->slurp on and get its contents.

Easy!

No TrackBacks

TrackBack URL: http://www.catalyzed.org/mt/mt-tb.fcgi/38

3 Comments

| Leave a comment

Thanks for the nicely written tutorial. Just a minor nit: If you want to be able to package your application with PAR along with the "share" data, you might want to use File::ShareDir::PAR instead. It should fall back to the normal File::ShareDir behaviour if not running in a PAR context.

This is a really useful module, however one place where it doesn't seem to help so much is for when you are developing and need to run code out of a locally developed module that hasn't been pushed to cpan yet. I've just seen hacks of different types for this problem. Like in Catalyst::Utils::home, it tries to figure out if you are in a developer mode basically by walking up the directories and looking for a Makefile.PL, while FormFu seems to just use the current working directory (I could easily be wrong).

Wonder if we could ever support something like XDG for share files and related?

http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

Steffen: I haven't done anything with PAR yet. Perhaps I could look into this and do a follow up, thanks for the comment.

John: Yea, there seem to be some hackish solutions to non-cpan or "make install"ed modules. I'll look at the XDG stuff, it sounds interesting. I've had some discussions with some people regarding perl packages in relation to debian packages, etc. We're lagging a bit :-/

Leave a comment

All comments are moderated. Spammers don't waste your time

Sponsored By


Ionzero: Rescue your dev project.
OpenID accepted here Learn more about OpenID

Following

Not following anyone

Note to spammers: all comments are moderated. Don't waste your time