話題のCatalystとやらにチャレンジしてみようと思っていましたが、この記事(qootas.org/blog)を頼りに試みるも失敗。
そんな中、たまたまGoogleで「ActivePerl Catalyst」で検索すると、見事にCatalyst PPM's for ActivePerl Windows v5.8.xを発見。
それでは今日ブックマークしていたElementary, ... use Catalyst qw(初挑戦);をトレースしつつ、動作確認をしてみたいと思います。
■インストール
ActivePerlの5.8系が入っているという前提。
コマンドプロンプトを開いて、ppmコマンドを実行します。別にプログラムメニューからPPMを実行しても可。
C:\>ppm PPM - Programmer's Package Manager version 3.1. (中略) ppm> install Module::Build ==================== Install 'Module-Build' version 0.2611 in ActivePerl 5.8.4.810. ==================== (中略) Successfully installed Module-Build version 0.2611 in ActivePerl 5.8.4.810. ppm> repository add Theoryx5 http://theoryx5.uwinnipeg.ca/ppms/ (中略) ppm> repository add Catalyst http://home.ngmedia.net/chansen/catalyst/ppms/ (中略) ppm> install Catalyst ==================== Install 'Exporter-Lite' version 0.01 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'UNIVERSAL-exports' version 0.03 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Path-Class' version 0.11 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Module-Pluggable-Fast' version 0.16 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Text-ASCIITable' version 0.15 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Test-Exception' version 0.21 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Tree-Simple' version 1.14 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Tree-Simple-VisitorFactory' version 0.05 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Tree-Simple' version 1.14 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Test-Simple' version 0.47 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'AppConfig' version 1.56 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'File-Spec' version 0.82 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'File-Temp' version 0.12 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Template-Toolkit' version 2.13 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Class-Data-Inheritable' version 0.02 in ActivePerl 5.8.4.810. ==================== (中略) ==================== Install 'Catalyst' version 5.23 in ActivePerl 5.8.4.810. ==================== (中略) Installing D:\usr\local\perl\bin\catalyst.bat Installing D:\usr\local\perl\bin\catalyst.pl Successfully installed Catalyst version 5.23 in ActivePerl 5.8.4.810. ppm>
ってな感じです。
一応アーカイブのバージョンが分かった方がいいと思って、記載しています。
あと、最後の二行を見ていただいても分かるとおりcatalyst.batとcatalyst.plがActivePerlのバイナリディレクトリに放り込まれていることは念頭においておいたほうがよいかも。
■スケルトン作成
作業ディレクトリ(ここではD:\Catalyst)でcatalyst.batを実行します。
D:\Catalyst>catalyst Hello created "Hello" created "Hello\script" created "Hello\lib" created "Hello\root" created "Hello\t" created "Hello\t\m" created "Hello\t\v" created "Hello\t\c" created "Hello\lib\Hello" created "Hello\lib\Hello\M" created "Hello\lib\Hello\V" created "Hello\lib\Hello\C" created "Hello\lib\Hello.pm" created "Hello/Build.PL" created "Hello/Makefile.PL" created "Hello/README" created "Hello/Changes" created "Hello\t/01app.t" created "Hello\t/02pod.t" created "Hello\t/03podcoverage.t" created "Hello\script/hello_cgi.pl" created "Hello\script/hello_fastcgi.pl" created "Hello\script/hello_server.pl" created "Hello\script/hello_test.pl" created "Hello\script/hello_create.pl"
とりあえずぼくはTemplate-Toolkitを使ったことがないんだけど、作例に基づいて行きます(笑)
D:\Catalyst>cd Hello D:\Catalyst\Hello>perl .\script\hello_create.pl view TT TT Couldn't load helper "Catalyst::Helper::View::TT", "Can't locate Catalyst/Helper /View/TT.pm in @INC (@INC contains: D:/usr/local/perl/lib D:/usr/local/perl/site /lib .) at (eval 34) line 3. " at D:/usr/local/perl/site/lib/Catalyst/Helper.pm line 148.
あらら、Catalyst::Helper::View::TTが無いと怒られてしまいました。
CPANからCatalyst-View-TT-0.12をゲットし、D:\usr\local\perl\site\lib\Catalystにコピー(この辺は皆さんのActivePerlのインストール先に依存します)して再チャレンジ。
D:\Catalyst\Hello>perl .\script\hello_create.pl view TT TT created "D:\Catalyst\Hello\lib\Hello\V\TT.pm" created "D:\Catalyst\Hello\t\v\tt.t"
あっさり出来ました(笑)
■テストアプリケーション
Elementary, ... use Catalyst qw(初挑戦);を引き続き参考にしつつ、テストアプリを作ってみました。
ソースはおんなじです。
修正したのは
- D:\Catalyst\Hello\lib\Hello.pm
- D:\Catalyst\Hello\root\page2.html
だけです。
■テストサーバ機能
さて、Catalystでも目立つテスト用HTTPdを実行してみます。
D:\Catalyst\Hello>perl .\script\hello_server.pl [Tue Jul 19 19:34:33 2005] [catalyst] [debug] Debug messages enabled [Tue Jul 19 19:34:33 2005] [catalyst] [debug] Loaded dispatcher "Catalyst::Dispa tcher" [Tue Jul 19 19:34:34 2005] [catalyst] [debug] Loaded engine "Catalyst::Engine::H TTP" [Tue Jul 19 19:34:34 2005] [catalyst] [debug] Found home "D:\Catalyst\Hello" Couldn't load "Hello::V::TT", "Can't locate Template/Timer.pm in @INC (@INC cont ains: D:/Catalyst/Hello/script/../lib D:/usr/local/perl/lib D:/usr/local/perl/si te/lib .) at D:/usr/local/perl/site/lib/Catalyst/View/TT.pm line 6. BEGIN failed--compilation aborted at D:/usr/local/perl/site/lib/Catalyst/View/TT .pm line 6. Compilation failed in require at (eval 27) line 3. ...propagated at D:/usr/local/perl/lib/base.pm line 85. BEGIN failed--compilation aborted at D:/Catalyst/Hello/script/../lib/Hello/V/TT. pm line 4. Compilation failed in require at D:/usr/local/perl/site/lib/Module/Pluggable/Fas t.pm line 82. " at D:/usr/local/perl/site/lib/Module/Pluggable/Fast.pm line 84. Compilation failed in require at .\script\hello_server.pl line 13. BEGIN failed--compilation aborted at .\script\hello_server.pl line 13.
わーい、落ちたー(笑)
今度はどうやらTemplate-Timerが無い模様。
さくっとコピーし再チャレンジ。
D:\Catalyst\Hello>perl .\script\hello_server.pl [Tue Jul 19 19:36:41 2005] [catalyst] [debug] Debug messages enabled [Tue Jul 19 19:36:41 2005] [catalyst] [debug] Loaded dispatcher "Catalyst::Dispa tcher" [Tue Jul 19 19:36:41 2005] [catalyst] [debug] Loaded engine "Catalyst::Engine::H TTP" [Tue Jul 19 19:36:41 2005] [catalyst] [debug] Found home "D:\Catalyst\Hello" [Tue Jul 19 19:36:42 2005] [catalyst] [debug] Loaded components .=----------------------------------------------------------------------------=.
Hello::V::TT |
[Tue Jul 19 19:36:42 2005] [catalyst] [debug] Loaded private actions .=-------------------------------------+--------------------------------------=.
Private | Class |
=-------------------------------------+--------------------------------------= |
/default | Hello |
/page2 | Hello |
[Tue Jul 19 19:36:42 2005] [catalyst] [debug] Loaded public actions .=-------------------------------------+--------------------------------------=.
Public | Private |
=-------------------------------------+--------------------------------------= |
/page2 | /page2 |
[Tue Jul 19 19:36:42 2005] [catalyst] [info] Hello powered by Catalyst 5.23 You can connect to your server at http://localhost:3000/
動いたー。感動。
ちなみにFirefoxだと、Hello.pmでContentTypeの設定を追加しないとplain textとして扱われていたので、追記しておきます。
sub default : Private { my ( $self, $c ) = @_; $c->res->content_type("text/html; charset=EUC-JP"); $c->res->output('<html><body>Hello World! <a href="page2/">go to page</a>.</body></html>'); }
とりあえず次はTinyURLでも実装してデータベース関係のフォローをしたいと思います。