Category: Diary Keywords: part-time job
今天接了另一份 Perl 兼职在干,也是做 Web Programming. 做的是一个客户支持系统。很常见的功能。客户有问题通过网页 open a ticket, 然后网站的客服人员通过回复将这个 ticket close 掉,并通过 email 通知客户。当然另外还有一些附加的功能。
这个系统类似 bug tracing 又类似 forum, 总体来说不算太难。所有的 Web Programming 看起来都是很类似的,无非将资料插入数据库又将资料从数据库取出来显示,或者更新或者删除。大部分工作就是这些所谓的 CRUD ( select, insert, update, delete ), 比较复杂的只是数据库的构造和资料的结构需要考虑。
没有使用 Catalyst 来做。但是用类似的概念,在 index.pl 进行了 action dispatch, 用 DBI 做 model, 用 TT 做 view.
用 YAML 来做 config 以避免 hard code. 其间将 $config 和 $query 作为 TT 的一个变量来处理。这样方便在 TT 模块中使用这些功能。大致的代码类似:
use vars qw/$query $dbh $tt $tt_vars $config/;增加 $tt_vars 的变量就用 $tt_vars->{message} 之类的,最后传到 $tt->process('message.tt', $tt_vars) || die $tt->error(); 就差不多了。
use FindBin;
my $DIR = $FindBin::Bin;
# config
$config = YAML::LoadFile("$DIR/config.yml");
# cgi
$query = new CGI;
# dbi
$dbh = DBI->connect(..
# template
$tt = Template->new({ ..
$tt_vars = {
cgi => $query,
config => $config,
};
不过这样做的效果似乎不是非常理想但还可以,毕竟 YAML 和 TT 都比较庞大。Any way, 这样写起来很顺畅。
TT tip
今天还碰到 TT 的变量连接。比如说 Perl 中可以这么写 $re = 'Re: ' . $title; 但是 TT 中因为 . 是用来作为 -> 使用的。所以变量连接可以这么写:
<input type='text' name='subject' value='[% cgi.param('subject') || "Re: ${ticket.ticket_subject}" %]' lenght='12' />"Re: ${ticket.ticket_subject}",用 ${} 来内插 TT 变量。