免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1905 | 回复: 3
打印 上一主题 下一主题

请教perl中dispatch方法的实现方法? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-13 16:30 |只看该作者 |倒序浏览
最近在看beginning python ,看到如下代码

class Handler:
    """
    An object that handles method calls from the Parser.

    The Parser will call the start() and end() methods at the
    beginning of each block, with the proper block name as a
    parameter. The sub() method will be used in regular expression
    substitution. When called with a name such as 'emphasis', it will
    return a proper substitution function.
    """
    def callback(self, prefix, name, *args):
        method = getattr(self, prefix+name, None)
        if callable(method): return method(*args)
    def start(self, name):
        self.callback('start_', name)
    def end(self, name):
        self.callback('end_', name)
    def sub(self, name):
        def substitution(match):
            result = self.callback('sub_', name, match)
            if result is None: match.group(0)
            return result
        return substitution

该代码中实现了所有名字为start_,end_,sub_开头的函数的抽象,比如start_html,start_heading,start_paragraph和end_html,end_heading.
我自己google了一下,类似的代码出现在imtermediate perl 和modern perl中,

my %start=
(
         $html=>\&start_html,
         $heading=>\&start_head,
         ...
}

my %end=
(...)

start_html{}
start_heading{}

使用的时候使用
for $element(@element){
         $start{$element}->();
         ...
}

我不知道有没有更接近于上述python中代码的实现,希望大家能够告诉我一下,谢谢!

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2010-11-13 20:02 |只看该作者
本帖最后由 zhlong8 于 2010-11-13 20:07 编辑

如果你理解 perl 的 OO 或者看完你上面说的 imtermediate perl 这根本就不是个问题。基本上用 perl 的语法把你上面的 Handle 照抄下就行,能用而且足够像。

楼主在 beginning python 难道楼主正在 python perl 同时入门中?

论坛徽章:
0
3 [报告]
发表于 2010-11-18 12:48 |只看该作者
我是做生物的,偶尔用bioperl来处理一些生物数据;使用python是因为pyMOL,所以二者都在用。都是入门级别的。看beginning python后面的项目的时候,试着用perl来实现,看看有什么不同,增加对perl的理解。
我刚开始的时候也是使用python的方法,比如:

sub start{
           my($name,@argv)=@_;
           
           my $start_name='start_'."$name";
           return \&{$start_name}->(@argv);
}

但是 strict 报告有问题,使用no strict  'refs'   后就没有问题了,但是总是觉得不好,也不perlish。

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2010-11-18 13:33 |只看该作者
本帖最后由 zhlong8 于 2010-11-18 13:39 编辑

回复 3# littlelittledog
  1. getattr(self, prefix+name, None)
复制代码
这样的代码就符合 python 的精髓吗?你要通过字符串找到相应的函数,这样是必须的,丑不丑是另一回事。perlish 还有一条, easy thing should be easy, hard thing should be possible. 更何况这个并不是什么难的事。你要理解为什么要 use strict; 它的目的只是为了方便你,而不是增加麻烦。就是为了应付这种情况 strict 才是 lexical 的,你完全可以在一个 BLOCK 中用 no strict  'refs'; 如果你看下 perl 自带的库就知道这样用有多广泛了,希望不要被自己框住了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP