2011年7月31日星期日

Open Perl Modules in Vim

在写程序的时候,用到一些perl模块时,经常需要查看这些模块的源代码。以前用的方法是新开一个screen窗口,module_info Module::Name获得模块的路径,然后再在vim里面打开之

久之感觉这样做颇没效率,于是决定写一个vim的函数来实现":Pme Module::Name"即可打开perl模块的功能。

将以下代码写入 .vimrc即可实现这个功能

function! OpenPerlModule(module) let module_path = system("perldoc -l " . a:module) if v:shell_error == 1 echo module_path return endif execute "e " . module_path endfunction command -nargs=1 Pme call OpenPerlModule()

Posted via email from Tech Blog of Woosley.Xu

2011年7月3日星期日

HTML::FormHandler介绍

  • 使用HTML::FormHandler生成表单

首先尝试创建一个用户登录表单

package Update; use Moose; use HTML::FormHandler::Moose;   #使用has_fields等语法糖 extends {HTML::FormHandler} use namespace::autoclean; use Digest::MD5 qw/md5_hex/;  has '+name' => (default => 'update');  #form的名字 has '+auto_fieldset' => (default => 0); #不在form内添加fieldset #创建表单元素的依赖关系 has '+dependency' => (default => sub{[[qw/password confirm/]]}); has_field 'name' => (type => 'Text', required => 1); has_field 'email' => (type => 'Email', required => 1); #使用apply transform加密密码 has_field 'password' => (type => 'Password', required => 1, apply => [{ transform => sub{ md5_hex(shift)}}], ); has_field 'confirm' => (type => 'Password', required => 1, apply => [{ transform => sub{ md5_hex(shift)}}], );  sub validate { my ($self) = @_; if ($self->field('confirm')->value ne

$self->field(‘password’)–>value) {

$self->field('password')->add_error("password not match"); } }  package Main; my $form = update->new(); $form->process(params => {name => 'woosley'}); print $form->render;     Name:  Email:  Email field is required Password:  Please enter a password in this

field

Confirm:  Please enter a password in this

field

这里定义了一个用户update的form, 表单名为update,由'+name'一行定义,'+‘在moose里面表示这个属性在父对象里面定义,在子对象中可以进行覆盖操作。form里面包含name,password,email,所有的元素都是必须的

使用这个表单只需要像Main包里面一样,调用$form->process(param => {postparams});即可,$form->render获得HTML代码。可以看到上面代码里面只传入了name,因此会得到一堆错误信息.一般来说如果表单验证提交出错,所有的错误信息都放在$form->errors里面,所以一个表单提交过程应该为

$form = Form->new(); unless($form->process(params => {})){ ##处理 $form->errors } ## 表单提交成功

HTML::FormHandler根据params参数是否为空来确定是否为表单提交,如果params为空,process也会返回失败,从而跳过表单提交后续部分工作。

对于每一个field的验证,可用使用 sub validate_fieldname {} 或者添加在has_field里面添加apply来实现,并定义出错信息

has_field 'email' => ( unique => 1, apply => [ { check => qr/^\w+\.\w+\@gmail\.com$/, message => 'Invalid Email' } ]);

还可以如上添加validate method在最后验证整个表单

如前一篇博文所述,FormHandler使用不同的模块控制如何生成表单HTML,上面例子给出的>是<div&gt形式,我们也可以使用table形式或者在每一个field外面什么都不加

has '+widget_wrapper'   => (default => 'Table'); has '+widget_form' => (default => 'Table');

http://search.cpan.org/~gshank/HTML-FormHandler-0.34001/lib/HTML/FormHandler/... 详细介绍了如何render

to be continued….

Posted via email from Tech Blog of Woosley.Xu

2011年7月2日星期六

HTML::FormHandler介绍

根据Catalyst的胖Model,瘦Controller原则,在controller里面应该使用最少的代码来保持整个逻辑的清爽。 但对于任何一个web app来说,最复杂的逻辑之一就是表单的提交,验证表单,之后再根据需要从做相关 的操作,查询数据库或者返回错误。而这些都是Controller的任务

如果在Controller里面hardcode这部分逻辑,整个代码会变得冗长而丑陋,HTML::FormHandler就是为了简化 表单的处理而编写的模块。HTML::FormHandler使用Moose作为底层的OO库,代码实现比较优雅和干净。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 
package SEA::Forms::User;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Model::DBIC';
use namespace::autoclean;
use Digest::MD5 qw/md5_hex/;

has '+name' => (default => 'user');
has '+auto_fieldset' => (default => 0);
has '+item_class' => (default => 'User');
has '+dependency' => (default => sub { [[qw/password confirm/]] });
has_field 'id' => (
    type => 'Text',
    required => 1,
    accessor => 'username',
    unique => 1,
    apply => [
        { check => qr/^\w{7}$/,
            message => 'Invalid username'
        }
    ],
    messages => {
        required => 'username is required',
        unique => 'This username is already taken'
    },
);

has_field 'email' => (
    unique => 1,
    messages => {unique => 'This email is already taken'},
    apply => [
        { check => qr/^\w+\.\w+\@ericsson\.com$/,
            message => 'Invalid Email'
        }
    ],
);

has_field 'password' => (
    type => 'Password',
    accessor => 'password',
    apply => [
        { transform => sub { md5_hex(shift) }
        }
    ],
);

has_field 'confirm' => (
    type => 'Password',
    apply => [
        { transform => sub { md5_hex(shift) }
        }
    ],
);

has_field 'submit' => (
    value => 'submit',
    type => 'Submit',
    css_class => 'submit'
);

sub validate {
    my ($self) = @_;
    if ($self->field('confirm')->value ne $self->field('password')->value) {
        $self->field('password')->add_error("password not match");
    }
}

__PACKAGE__->meta->make_immutable;

HTML::FormHandler主要分为这么几个部分

  • HTML::FormHandler::Field::*

Field主要用来定义表单单元的类型,对某一种类型,会有相应的验证方法和属性,比如定义一个Text类型单元

has_field 'name'  => (Type => 'Text', maxlength => 30, minlength =>

6);

  • HTML::FormHandler::Widget::*

Widget用来生成Field的HTML代码,查看源代码,每一个widget都有个render方法,用来创建wdiget的HTML code,这里有两个特殊的widget。HTML::FormHandler::Widget::Wrapper::*,用来添加包裹每个表单单元的HTML,比如添加的是div

<input name="name", type="text">

还有一个是Widget::Form,定义…..部分如何生成

  • HTML::FormHandler:Moose

使用Moose定义的语法糖,比如has_field

HTML::FormHandler::Model::

与数据库交互的模块,值得注意的是FormHandler没有添加任何数据库操作的代码,而只是使用Moose Role定义了数据库操作所需要的方法。

to be continued…

Posted via email from Tech Blog of Woosley.Xu