[PerlChina] perl解析HTML处理成DOM对象的处理
Qiang ( James ) Li
shijialee at gmail.com
Mon Nov 26 20:04:36 PST 2007
rorot wrote:
> 谢谢诸位, 特别是Qiang, jzhang的热心解答!
>
> 我之于Mechanize模块的尝试,实际是出于兴趣。因为对Javascript较熟悉,以前
> 用HTML::TreeBuilder模块时,虽然也可以达到解析HTML文档之目的,然终究想用
> 一套和DOM类似的接口来操作HTML Syntax Document。
>
> 事实上,若要模仿浏览器行为,我想WWW::*模块足以胜任。如jzhang提到的,
> Mechanize主要提供的是模拟浏览器行为的功能。那么在这里我的疑惑是: 既然有
> 了WWW::*诸如此类模块,那么WWW::Mechanize, Mozilla::Mechanize存在的目的是
> 什么?我唯一能说服的就是他们提供了和DOM类似的接口。使用起来更熟悉。诸位
> 如何看?
>
TIMTOWTDI.. :)
Mozilla::Mechanize - Like WWW::Mechanize but using Gtk2::MozEmbed
> 下面附上Mozilla::Mechanize的make test输出结果。在make test失败的情况下,
> 我强制make install,然后编写了测试Mozilla::Mechanize的代码,运行后脚本崩
> 溃, 提示段错误。
>
> Failed 7/8 tests, 12.50% okay
> Failed Test Stat Wstat Total Fail List of Failed
> -------------------------------------------------------------------------------
> t/00-props.t 0 139 11 20 2-11
> t/01-construct.t 0 139 13 24 2-13
> t/02-ct.t 0 139 20 38 2-20
> t/basic.t 0 139 12 22 2-12
> t/field.t 0 139 12 22 2-12
> t/find_link.t 0 139 60 118 2-60
> t/formbasics.t 0 139 33 64 2-33
> t/frames.t 0 139 9 16 2-9
> t/image-parse.t 0 139 20 38 2-20
> t/jstest.t 0 139 4 6 2-4
> t/select.t 0 139 26 50 2-26
> t/swaplist.t 0 139 25 48 2-25
> t/tick.t 0 139 8 14 2-8
> 2 tests skipped.
> Failed 13/15 test scripts. 240/253 subtests failed.
> Files=15, Tests=253, 4 wallclock secs ( 1.97 cusr + 0.27 csys = 2.24 CPU)
> Failed 13/15 test programs. 240/253 subtests failed.
> make: *** [test] Error 255
> rorot at rorot-laptop:~/.cpan/build/Mozilla-Mechanize-0.05$
>
:( 帮不了什么忙。。
> ------------------------------------------------------------------------------------------------
> 另: 这里有一个返回引用的问题,我一直疑惑,请教诸位。
> 在我的一个package里,我这么写道:
>
> sub new {
> my $class = shift;
> my $self = {@_};
> bless ($self, $class);
> $self->_init;
> return $self;
> }
>
> sub _init {
> my $self = shift;
> #return if (exists %$self{'dbh'});
> $self->{'dbh'} = DBI->connect($DBD, $username, $password) || die
> "Connect Database Error";
> $self->{'dbh'}->{unicode} = 0;
> }
>
> sub DESTROY {
> my $self = shift;
> $self->{'dbh'}->disconnect if $self->{'dbh'};
> }
>
> sub dbh {
> my $self = shift;
> return $self->{'dbh'};
> }
> -------------------------------------------------------------------------------------------------
>
> 其中的dbh()方法,是我为了在别的方法里调用方便时写的。比如在该pakeage的其
> 他方法里,我可以这么调用dbh: $self->dbh->query($query);
>
> 但问题是,我不知道在sub dbh()方法里return $self->{'dbh'}, 是不是又临时拷
> 贝了一份dbh对象,还是直接返回了dbh对象的引用(不是指针). 我这样写的dbh
> ()方法,是否有多生成临时对象的嫌疑? 或者为了方便在别的函数里使用$self->
> dbh->method的形式,而又更好的撰写dbh()函数的途径?( 主要是为了避免临时对
> 象的生成,不知道在perl里面是如何处理的?)
>
引用。dbh() 返回的还是 new 对象时生成的 dbh. 我一般在每一个 project 里有
一个简单的 DB.pm 只是返回 dbh 或 DBIx::Simple 的 object.
在 mod_perl 下我用 DBI 的 connect_cached 而不是 connect, 作用类似于
Apache::DBI.
Qiang
More information about the China-pm
mailing list