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.

http://search.cpan.org/~jrobinson/DBIx-Class-0.07001/lib/DBIx/Class/ResultSet.pm#single

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']);
__PACKAGE__->setup;


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=Log::Dispatch::File
log4perl.appender.Log.filename=../log/foorum.log
log4perl.appender.Log.mode=append
log4perl.appender.Log.layout=PatternLayout
log4perl.appender.Log.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
and script like:
# Log4perl
use Catalyst::Log::Log4perl;
__PACKAGE__->log(Catalyst::Log::Log4perl->new('../logger.conf'));
__PACKAGE__->setup;
but the foorum.log shows:
2006/08/21 19:10:55 DEBUG> Catalyst.pm:1457 Catalyst::handle_request - debug
2006/08/21 19:10:55 INFO> Catalyst.pm:1457 Catalyst::handle_request - info
the "Catalyst.pm 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.

Updated

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. http://fayland.googlecode.com/svn/trunk/MISC/patches/EMail_Send_SMTP.patch
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
email:
- SMTP
- smtp.gmail.com
- tls
- 1
- Hello
- 'www.fayland.org'
- Port
- 587
- User
- [email protected]'
- Password
- yourpassword
and
$c->email(
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->content_type('application/octet-stream');
$c->res->headers->header( 'Content-Disposition' => "attachment; filename=\"fayland.zip" );

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

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

$c->res->body($data);
}
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/) {
$r->filename('E:\Fayland\Perl\fayland.txt');
return Apache2::Const::DECLINED;
}

return Apache2::Const::DECLINED;
}

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

conf 里的:
<Perl>
use lib 'E:/Fayland/';
</Perl>
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' %]
ok
[% ELSE %]
no
[% END %]
输出的是 no

正确的用法应当是用 Template::Plugin::String::Compare
[% USE String.Compare %]
[% IF String.Compare.new('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!

HTML::BBCode

12 August 2006


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

附上一个我的 patch 地址:http://fayland.googlecode.com/svn/trunk/MISC/patches/BBCode.patch
可以支持
[font=宋体]文字[/font]
[flash]http://somewhere.com/some.swf[/flash]
[align=center]place it in center[/algin]
[music]http://somewhere.com/anymusic.(mp3|wma|rm|ra|..)[/music]

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 http://svn.perlchina.org/ 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.

Thanks.

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 http://svn.bulknews.net/repos/plagger/trunk/plagger
since I think we need the templates under the dir "assets".

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

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

# Load 'Bundle' planet plugins
- module: Bundle::Planet
config:
duration: 3 days
title: Fayland's Planet
description: What I am interested in
dir: E:/Fayland/plagger
url: http://fayland.vicp.net/plagger
theme: sixapart-std
stylesheet:
- http://planet.catalystframework.org/css/global/planet.css
- http://planet.catalystframework.org/css/global/generic.css
- http://planet.catalystframework.org/css/global/layout.css
then run the script plagger. it will make a "index.html" and some xml files under the dir "E:/Fayland/plagger/".

try to visit http://fayland.vicp.net/plagger 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.

http://planet.catalystframework.org/

and RSS is avaiable.

PS, I'm in ZhengZhou now.