search->first, find, and single

25 August 2006

DBIx::Class 简单地读取一条数据的时候有三种方法。

一是用 ->search( { author_id => 1 } )->first;
二是用 ->find( { author_id => 1 } );
三是用 ->single( { author_id => 1 } );

我用 Data::Compare 比较过。三者的数据是一样的。
但是原理上来说,最下面的是最快的。因为没有创建一个 Cursor.

ConfigLoader and Log::Log4perl

21 August 2006

it seems that ConfigLoader and Log::Log4perl in Catalyst are not such perfect.

ConfigLoader cann't load more than 2 yaml files like "a.yml", "b.yml", "c.yml".(Am I missing something?) but Config::YAML does.
use Catalyst qw/ .. Config::YAML ../;
__PACKAGE__->config('config_file' => ['a.yml', 'b.yml', 'c.yml']);

and when I setup the Catalyst::Log::Log4perl up, but it sucks. the log conf is something like:
log4perl.logger.Foorum=DEBUG, Log

log4perl.appender.Log.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
and script like:
# Log4perl
use Catalyst::Log::Log4perl;
but the foorum.log shows:
2006/08/21 19:10:55 DEBUG> Catalyst::handle_request - debug
2006/08/21 19:10:55 INFO> Catalyst::handle_request - info
the " 1457 Catalyst::handle_request" is useless. mm, Am I missing something too?

GMail and Email::Send

20 August 2006

I use GMail in my daily life. and the GMail SMTP is using TSL

The Email::Send seems not to support TSL. and Catalyst::Plugin::Email is using Email::Send.


I haven't noticed until today that Ricardo SIGNES has applied this patch as Email::Send 2.180.
So I removed the link in fayland.googlecode.

so I create a patch to Email::Send::SMTP.
but the module Net::SMTP::TLS breaks some function of Net::SMTP (like don't return a true value of ->mail, ->to only support one scalar and no param, etc.)
I sent the patch to the author of Email::Send, but I'm not sure whether he will accept it because of the Net::SMTP::TLS.

the yml config of Catalyst::Plugin::Email should something like
- tls
- 1
- Hello
- ''
- Port
- 587
- User
- '[email protected]'
- Password
- yourpassword
header => [
From => '[email protected]',
To => '[email protected]',
Subject => 'Test Foorum Email'
body => 'OK',

Have fun!

Server zip file in Catalyst with IO::Scalar

19 August 2006

Thank God, Thank IO::Scalar.

u know we can transfer a filehandle to $c->res->body. and IO::Scalar is making my life easy.

just a snippet code, have fun.
sub test : Local {
my ( $self, $c ) = @_;

use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

my $zip = Archive::Zip->new();

$c->res->headers->header( 'Content-Disposition' => "attachment; filename=\"" );

use IO::Scalar;
my $data;
my $fh = new IO::Scalar \$data;

$zip->addFile('E:/Fayland/index.html') or die $!;

IO::Scalar 能将一个 $scalar 作为 filehandle IO 来对待。在 Catalyst 里将是特别有用的。

an example of PerlMapToStorageHandler

15 August 2006

PerlMapToStorageHandler 是个新的东西。在 2.0.2 才从 PerlTransHandler 里剥离出来,做了一些性能方面的提升。
大致的用途就是改变 uri 所对应的文件。

比如我们的 DocumentRoot 是 E:/Fayland, 然后 http://localhost/test.txt 本该访问的是 E:/Fayland/test.txt 但是我们想根据某一条件或者什么乱七八糟的事让这个 url 实际上对应的文件是个 E:/Fayland/Perl/fayland.txt 就可以用 MapToStorage 里的 filename 这函数。最最简单的例子(没有任何判断条件):
package Apache2My::FileMapping;

use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(OK DECLINED);

sub handler {
my $r = shift;

my $uri = $r->uri;

if ($uri =~ /test\.txt/) {
return Apache2::Const::DECLINED;

return Apache2::Const::DECLINED;

Download it
这样的代码写后,即使 E:/Fayland/test.txt 是存在的,但是这 url 实际上出来的内容还是 Perl/fayland.txt 里的内容。

conf 里的:
use lib 'E:/Fayland/';
PerlMapToStorageHandler Apache2My::FileMapping


TT String Compare

12 August 2006

Template-Toolkit 里是没有 gt 这样的字符串比较操作符的。

像这样的东西,在 Perl 里是正确的。
if ('2006-08-12 06:32:38' gt '2006-08-11 16:43:41') {
print 'ok';
} else {
print 'no';
会输出 ok

但在 TT 里用 > 比较是错误的:
[% IF '2006-08-12 06:32:38' > '2006-08-11 16:51:08' %]
[% ELSE %]
[% END %]
输出的是 no

正确的用法应当是用 Template::Plugin::String::Compare
[% USE String.Compare %]
[% IF'2006-08-12 06:32:38') > '2006-08-11 16:51:08' %]

String.Compare 用 overload 重载了比较操作符:
use overload (
'<' => \&_lt,
'>' => \&_gt,
'<=' => \&_le,
'>=' => \&_ge,
fallback => 1,

Have fun!


12 August 2006

很多 Forum 都用 BBCode, 我也打算用这个。这样比较好控制。
CPAN 上有一个 HTML::BBCode 但是跟我要求的有一点点差别。我需要 flash, music, font, align 的支持。所以就对 HTML::BBCode 做了一点点修改。(BBCode::Parser 这模块太复杂了,HTML::BBCode 就比较简单。)
这个修改非常简单,因为原来的 HTML::BBCode 写的就非常棒。

附上一个我的 patch 地址:
[align=center]place it in center[/algin]

PerlChina and GoogleCode

11 August 2006

yes, I setup up the Google Code for PerlChina, perlchina.

Now it has one project located: p6l10n - Translation for Perl 6 Synopsis.
yes, I do know we have already. but it doesn't matter I think.

If you are interesting in, please join us.
PS, you can take over the administrator work if you like.


Plagger rocks, Bloglines sucks

10 August 2006

I export the OPML from Bloglines, then switch my RSS reader as Plagger.
I must admit that the installation of Plagger is painful (in Win32), there is a lot of modules needed. mm, I don't use PPM, cpan just take me to the place.

I think you'd better svn checkout
since I think we need the templates under the dir "assets".

my config YAML are something like:
# Set timezone to your local one
timezone: Asia/Shanghai
# this make logger logs important information
level: info

- module: Subscription::OPML
url: http://localhost/plagger/export.opml

# Load 'Bundle' planet plugins
- module: Bundle::Planet
duration: 3 days
title: Fayland's Planet
description: What I am interested in
dir: E:/Fayland/plagger
theme: sixapart-std
then run the script plagger. it will make a "index.html" and some xml files under the dir "E:/Fayland/plagger/".

try to visit to have a general idea. (PS, the host is my own machine, so it's only available in 9:00AM to 8:00PM).

when I need to read the lastest items of the RSS articles, I need to run the script plagger again to fetch them remotely.

mm, any way, Plagger is great.

Plagger + Catalyst = Planet

06 August 2006

we do have a Planet Catalyst now, powered by Plagger.

and RSS is avaiable.

PS, I'm in ZhengZhou now.