Devin Austin: June 2009 Archives

Everyone's got their preferred "stack" of modules + frameworks they like to use for any big project they use. I typically break mine down into a (web) framework, an ORM (or database interaction layer if you don't like ORMs), a templating system, caching, reverse proxy, application server (for instance, FastCGI for web stuff), web server software and version control.

Mine are:

  • Framework: Catalyst
  • ORM: DBIx::Class
  • Templating: Template::Toolkit -
  • Caching: memcached
  • Reverse proxy: Varnish (caching too)
  • Application server: FastCGI
  • Web server: nginx
  • Version control: git

Those may or may not be grouped together properly, but it's the best i could think of. Being a web developer, mine are all network/web oriented. Let's see yours! Add them to the Wiki Extras for this post, or just comment below!

We love Moose.

We love email.

Let's put them together!

The idea:

I'm working on a website where email is going to be used extensively. Like most sites, I'll need registration confirmation, email updates to users, confirm password change, and future things like emailing photos in, etc.

I've used other modules for email and have never really been happy with them. I came upon Email::Stuff recently, and decided to give it a whack. However, I didn't want to have to set it up over and over everywhere it needed to be used. So this is where Moose came in.

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!

Sponsored By


Ionzero: Rescue your dev project.

Following

Not following anyone

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