mod_perl, grrr

sumdeus on 2004-02-25T18:08:06

I've been messing around with mod_perl and template toolkit for the last few days. I've been reading through a lot of information on the both. Before I started this, I knew how they both worked, but had never actually created a mod_perl installation from scratch. Now I have my own!

So I'm really only trying to do something simple with them both to begin, I want the following:

  • Requests to /foo.htm, which is actually a template, is taken care of by mod_perl
Seems simple enough? Here's the setup I have right now... all requests to /foo.html are converted using mod_rewrite to /mod-perl/handler.pl?file=foo.html. So I have Apache::Registry as the real "handler".

What I'd really like, is to have my own handler, which I "attempted" without success. You will want to ask, "what was the problem"? Of course, I'd answer, with a smart ass smile, "it didn't work!". No,no, the problem was that it didn't seem like it made sense in this case... perhaps overkill?

I just don't know the most appropriate way to map a request for an html file through a custom mod_perl handler to template toolkit. Is there some 'industry' standard? I can't find any useful examples, all I find are the generic "do-this" tutorials... even in the safari/oreilly books. Maybe I'm browsing the wrong one!

Any hints ,URLS's, or book names would be greatly appreciated for this mod_perl administrating neophyte.


Apache::Template

merlyn on 2004-02-25T18:27:44

See Apache::Template - like Apache::Registry, but for templates instead of CGI.pm scripts.

Re:Apache::Template

sumdeus on 2004-02-25T19:16:05

Thanks Randal. Now all I have to do is figure out how in the hell to get it installed without gcc on the machine.

Without gcc?

rafael on 2004-02-25T21:24:59

What's your OS ? and how did you install mod_perl without gcc :) ?

Re:Without gcc?

sumdeus on 2004-02-25T21:27:33

Dare I say it's a hosting company... where they/I have about 200 perl modules installed, but for some reason this isn't one of them. They don't have gnu tools for security reasons, of course for another 15 bucks a month I can get them. I'm stuck with them for another 1/2 a year, so I have to make due. Unfortunately I need gcc for the xs stuff.

mapping URLs to template files

perrin on 2004-02-26T17:03:35

First off, I am not a fan of Apache::Template, or of mapping your URLs directly to the name of a template file. I think the URL and the name of the file you're going to run should have only a loose mapping, therefore I usually just map my URL to a handler, and then let the handler decide which template to run based on what it's doing. For example, requesting /login/ might result in running login_form.html, or maybe login_error.html, etc.

However, if I were trying to do what you are doing (map /foo.html to a handler that will run a template called foo.html), the easiest approach is the Files directive:

  <Files *.html>
    SetHandler  perl-script
    PerlHandler My::TT::Handler
  </Files>
Then in My::TT::Handler you just look at the URI (see the documentation for the $r request object on the mod_perl site) and decide which file to run through TT based on that.

This being apache, there are 1000 other ways to do it. Some are in the new TT book, one of them in on-line here, and all of the ways people do it with other systems like HTML::Template apply to TT as well.

Re:mapping URLs to template files

sumdeus on 2004-02-26T17:42:05

Very helpful, and nice since I don't need to install any additional modules. I will goof around with the handler... I was using Location directive instead of the Files so things were a little goofy.

Appreciate the comment.