首页 > Perl > 搭建第一个Catalyst应用实例

搭建第一个Catalyst应用实例

2012年3月22日 16点39分 发表评论 阅读评论

记录搭建第一个Catalyst应用实例的步骤。前面安装的Catalyst::Devel中已经包括了用来输出HTML的Template Toolkit,现在需要安装的是Catalyst和它的接口Catalyst::View::TT,

# cpan -i Catalyst::View::TT

根据Catalyst::View::TT的标准,在View目录下生成TT.pm文件:

phillip@athens:~/MyApp$ perl script/myapp_create.pl view TT TT
phillip@athens:~/MyApp$ ls lib/MyApp/View/
TT.pm

获取Catalyst::View::TT的版本信息:

$ perl -MCatalyst::View::TT -e 'print Catalyst::View::TT->VERSION'
0.38

修改Makefile.PL文件,在’require’区域后面加上‘Catalyst::View::TT’:

phillip@athens:~/MyApp$ vim Makefile.PL

修改后的Makefile.PL文件:

#!/usr/bin/env perl
# IMPORTANT: if you delete this file your app will not work as
# expected.  You have been warned.
use inc::Module::Install 1.02;
use Module::Install::Catalyst; # Complain loudly if you don't have
                               # Catalyst::Devel installed or haven't said
                               # 'make dist' to create a standalone tarball.

name 'MyApp';
all_from 'lib/MyApp.pm';

requires 'Catalyst::Runtime' => '5.90011';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'Moose';
requires 'namespace::autoclean';
requires 'Config::General';
# This should reflect the config file format you've chosen
requires 'Catalyst::View::TT' => '0.38';
# See Catalyst::Plugin::ConfigLoader for supported formats
test_requires 'Test::More' => '0.88';
catalyst;

install_script glob('script/*.pl');
auto_install;
WriteAll;

在MyApp/root目录中创建输入的HTML模板,将模板文件命名为’phillip.tt’, 其代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="zh_ch">
<head>
<title>Phillip的哲学世界</title>
</head>
<body>
<h1>Phillip的哲学世界</h1>
<p>
        哪只球队将获得是今年的意甲冠军? 
        [% word | html %].
</p>
</body>
</html>

现在phillip.tt还不能做任何事,因为没有定义与之关联的应用。下面将生成一个名字为”Phillip”的控制器:

phillip@athens:~/MyApp$ perl script/myapp_create.pl controller Phillip
 exists "/home/phillip/MyApp/script/../lib/MyApp/Controller"
 exists "/home/phillip/MyApp/script/../t"
created "/home/phillip/MyApp/script/../lib/MyApp/Controller/Phillip.pm"
created "/home/phillip/MyApp/script/../t/controller_Phillip.t"

生成在Phillip.pm文件在/lib/MyApp/Controller目录下。

phillip@athens:~/MyApp/lib/MyApp/Controller$ ls
Phillip.pm  Root.pm

修改Phillip.pm,使用前面的phillip.tt模板:

package MyApp::Controller::Phillip;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME
MyApp::Controller::Phillip - Catalyst Controller

=head1 DESCRIPTION
Catalyst Controller.

=head1 METHODS
=cut
=head2 index
=cut

sub phillip : Global {
        my ( $self, $c, @args ) = @_;
        my $word = $args[0] || 'Barnet';
        $c->stash->{template} = 'phillip.tt';
        $c->stash->{word}= $word;
}

=head1 AUTHOR
phillip,,,

=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
__PACKAGE__->meta->make_immutable;
1;

可以使用myapp_test测试一下:

$ perl script/myapp_test.pl /phillip/milan
[debug] Debug messages enabled
[debug] Statistics enabled
[debug] Loaded plugins:
.----------------------------------------------------------------------------.
| Catalyst::Plugin::ConfigLoader  0.30                                       |
'----------------------------------------------------------------------------'

[debug] Loaded dispatcher "Catalyst::Dispatcher"
[debug] Loaded engine "Catalyst::Engine"
[debug] Found home "/home/phillip/MyApp"
[debug] Loaded Config "/home/phillip/MyApp/myapp.conf"
[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class                                                           | Type     |
+-----------------------------------------------------------------+----------+
| MyApp::Controller::Phillip                                      | instance |
| MyApp::Controller::Root                                         | instance |
| MyApp::View::TT                                                 | instance |
'-----------------------------------------------------------------+----------'

[debug] Loaded Private actions:
.----------------------+--------------------------------------+--------------.
| Private              | Class                                | Method       |
+----------------------+--------------------------------------+--------------+
| /default             | MyApp::Controller::Root              | default      |
| /end                 | MyApp::Controller::Root              | end          |
| /index               | MyApp::Controller::Root              | index        |
| /phillip/phillip     | MyApp::Controller::Phillip           | phillip      |
'----------------------+--------------------------------------+--------------'

[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path                                | Private                              |
+-------------------------------------+--------------------------------------+
| /                                   | /index                               |
| /...                                | /default                             |
| /phillip/...                        | /phillip/phillip                     |
'-------------------------------------+--------------------------------------'

[info] MyApp powered by Catalyst 5.90011
[info] *** Request 1 (1.000/s) [13033] [Thu Mar 22 17:06:34 2012] ***
[debug] Path is "phillip"
[debug] Arguments are "milan"
[debug] "GET" request for "phillip/milan" from "127.0.0.1"
[debug] Rendering template "phillip.tt"
[debug] Response Code: 200; Content-Type: text/html; 
charset=utf-8; Content-Length: 326
[info] Request took 0.052756s (18.955/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /phillip/phillip                                           | 0.000292s |
| /end                                                       | 0.046260s |
|  -> MyApp::View::TT->process                               | 0.044756s |
'------------------------------------------------------------+-----------'

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="zh_ch">
<head>
<title>Phillip的哲学世界</title>
</head>
<body>
<h1>Phillip的哲学世界</h1>
<p>
	哪只球队将获得今年的意甲冠军?
	milan.
</p>
</body>
</html>

注意,此时如果在浏览器中访问http://catalyst_ip_or_name:3000/phillip/milan将报错’Page not found’。重新启动Catalyst开发web服务’myapp_server.pl’仍然出现同样的情况。必须要进行编译和安装模块:

phillip@athens:~/MyApp$ perl Makefile.PL
include /home/phillip/MyApp/inc/Module/Install.pm
include inc/Module/Install/Metadata.pm
include inc/Module/Install/Base.pm
include inc/Module/Install/Makefile.pm
Cannot determine perl version info from lib/MyApp.pm
include inc/Module/Install/Catalyst.pm
include inc/Module/Install/Include.pm
include inc/File/Copy/Recursive.pm
*** Module::Install::Catalyst
Please run "make catalyst_par" to create the PAR package!
*** Module::Install::Catalyst finished.
include inc/Module/Install/Scripts.pm
include inc/Module/Install/AutoInstall.pm
include inc/Module/AutoInstall.pm
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- Test::More                       ...loaded. (0.98 >= 0.88)
- Catalyst::Runtime                ...loaded. (5.90011 >= 5.90011)
- Catalyst::Plugin::ConfigLoader   ...loaded. (0.30)
- Catalyst::Plugin::Static::Simple ...loaded. (0.29)
- Catalyst::Action::RenderView     ...loaded. (0.16)
- Moose                            ...loaded. (2.0402)
- namespace::autoclean             ...loaded. (0.13)
- Config::General                  ...loaded. (2.50)
- Catalyst::View::TT               ...loaded. (0.38 >= 0.38)
*** Module::AutoInstall configuration finished.
include inc/Module/Install/WriteAll.pm
include inc/Module/Install/Win32.pm
include inc/Module/Install/Can.pm
include inc/Module/Install/Fetch.pm
Writing Makefile for MyApp
Writing MYMETA.yml and MYMETA.json
Writing META.yml
phillip@athens:~/MyApp$ make && make test

注意,make install需要root权限,否则报错:

phillip@athens:~/MyApp$ make install
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR: Can't create '/usr/local/bin'
Do not have write permissions on '/usr/local/bin'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 at -e line 1
make: *** [pure_site_install] 错误 13

使用sudo安装模块:

phillip@athens:~/MyApp$ sudo make install
[sudo] password for phillip: 
Installing /usr/local/share/perl/5.12.4/MyApp/Controller/Phillip.pm
Installing /usr/local/share/perl/5.12.4/MyApp/root/phillip.tt
Installing /usr/local/man/man3/MyApp::Controller::Phillip.3pm
Appending installation info to /usr/lib/perl/5.12/perllocal.pod

现在,在浏览器中可正常访问http://catalyst_ip_or_name:3000/phillip/milan。如果更新了/home/phillip/MyApp/root/phillip.tt模板,变化的内容即使重新启动Catalyst web服务器也无法显示。修改后可以重新编译,或者直接对’usr/local/share/perl/5.12.4/MyApp/root/phillip.tt’进行修改也可以。

还有一点需要注意的,Catalyst web服务器启动最好使用:

$ perl script/myapp_server.pl -r -d

‘-r’参数将使服务在恰当的时候自动重启,比如当发现Phillip.pm时:

Saw changes to the following files:
 - /usr/local/share/perl/5.12.4/MyApp/Controller/Phillip.pm (create)
Attempting to restart the server
分类: Perl 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.