2011年12月16日星期五

Re: [PerlChina] Getopt::Euclid

我的测试,性能不错!

This is perl 5, version 14, subversion 1 (v5.14.1) built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Binary build 1401 [294969] provided by ActiveState http://www.ActiveState.com
Built Jun 16 2011 18:54:40

2011/12/14 许高飞 <flyhighxu@gmail.com>
大家跑demo-euclid.pl慢吗?我这儿有时候还好有时候很慢,操作系统是Windows XP,Perl版本
C:\>perl -v

This is perl, v5.10.0 built for MSWin32-x86-multi-thread


在 2011年12月4日 下午6:48,joe jiang <lamp.purl@gmail.com>写道:
Hi I have made one for the 4th day:)

=for advent_year 2011

=for advent_day 4

=for advent_title Getopt::Euclid

=for advent_author Joe Jiang

写脚本的时候,最麻烦的两件事,无非是写文档和设计用户接口。有了 Getopt::Euclid,这两件事情居然开始变得十分有趣了。应该说,文档驱>动的程序,这样的创意并不少见,可是用来作为用户接口,这真的还是让人觉得十分恰当。

相比其他的脚本参数分析模块,这个模块的知名度比较低,不过它的作者 Damian Conway 还是十分知名的。现在的维护者 Florent Angly 看起来
也是参数模块这方面的爱好者,一同被他收纳的还有 Getopt::Declare。

具体的使用方法可以参考以下的测试代码:

=begin code

    #!/usr/bin/perl -wl

    use Getopt::Euclid;
    print for @{$ARGV{-i}};

    =head1 NAME

    demo-euclid - testing of module Getopt::Euclid

    =head1 VERSION

    This documentation refers to demo-euclid version 0.0.1

    =head1 USAGE

        demo-euclid [options]  -i[n][file] <file>

    =head1 REQUIRED ARGUMENTS

    =over

    =item  -i[n][file] <file>

    Specify input file

    =for Euclid:
        repeatable
        file.type:    readable
        file.default: '-'

    =back

=end code

有了以上的代码,你就可以这样测试一下了:

=begin code

    $ perl demo-euclid.pl --version
    This is demo-euclid.pl version 0.0.1

=end code

看上去还不错,这个工具脚本已经可以自动汇报版本号了!然后再测试一下正式的功能:

=begin code

    $ perl demo-euclid.pl -i /etc/passwd -i /etc/group
    /etc/passwd
    /etc/group

=end code

这正是我们的工具需要的功能,也就是分行(因为 #! 行的末尾有个 l 选项)打印文件名。另外,文件不存在,或者不可读的时候,脚本还会自>动报错:

=begin code


    $ perl demo-euclid.pl -i /etc/pass
    Invalid "-i[n][file] <file>" argument.
    <file> must be readable but the supplied value ("/etc/pass") isn't.
    (Try: demo-euclid.pl --help)

    $ perl demo-euclid.pl --help
    Usage:

    demo-euclid.pl -i <file>
    ...

=end code

看上去还挺专业的,连相应的帮助信息也有了。衷心希望你会喜欢上这个小模块!


--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

[PerlChina] 2011 CN Perl Advent Day 17: DB::Skip

=for advent_year 2011

=for advent_day 17

=for advent_title DB::Skip for Debugging

=for advent_author Joe Jiang

有时候,天气不好、手发潮、皮肤过敏,你的老板在脖子边上咆哮,这样的时候写出的代码都特别需要调试器的支持。

而且,更多时候,你实在容易迷失在一段别人写的 Perl 代码里面,于是你感觉快要进入之前说的倒霉状态了。这时候你需要的,其实还是调试器。

=begin code

    $ perl -MCalendar::Simple=date_span -lde 'date_span(mon => 12, year => 2011,  begin => 1, end => 25)'
   
    Loading DB routines from perl5db.pl version 1.33
    Editor support available.
   
    Enter h or `h h' for help, or `man perldebug' for more help.
   
    main::(-e:1):   date_span(mon => 12, year => 2011,  begin => 1, end => 25)
      DB<1> s
    Calendar::Simple::date_span(/usr/share/perl5/Calendar/Simple.pm:157):
    157:      my %params = @_;
      DB<1>
    Calendar::Simple::date_span(/usr/share/perl5/Calendar/Simple.pm:159):
    159:      my @now = (localtime)[4, 5];
      DB<1>
    Calendar::Simple::date_span(/usr/share/perl5/Calendar/Simple.pm:161):
    161:      my $mon   = $params{mon}   || ($now[0] + 1);
      DB<1>
    ...
    ...
    ...
    Calendar::Simple::date_span(/usr/share/perl5/Calendar/Simple.pm:167):
    167:      my @cal = calendar($mon, $year, $start_day);
      DB<1>  # 这里进入了一个新的函数,叫做 calendar
    Calendar::Simple::calendar(/usr/share/perl5/Calendar/Simple.pm:69):
    69:       my ($mon, $year, $start_day) = @_;
    ...
    ...
    ...
      DB<1>
    Calendar::Simple::calendar(/usr/share/perl5/Calendar/Simple.pm:85):
    85:         $first = DateTime->new(year => $year,
    86:                                month => $mon,
    87:                                day => 1)->day_of_week % 7;
      DB<1>  # 这里进入了一个新的模块,叫做 DateTime
    DateTime::new(/usr/lib/perl5/DateTime.pm:199):
    199:        my $class = shift;
    ...
    ...
    ...
   
=end code

于是,一个个新模块被跟踪进去(前方还有 Class::Singleton、Params::Validate 等著名模块),直到最后彻底放弃,宣告今天不适合工作。

其实,你心里知道 DateTime 这个模块应该早已做过许多测试,问题不大,可是又懒得去查 Perl Debug 手册,不知道 r 命令就能返回摸个函数的上级调用。

没错,这样的场合,在第 85 行其实可以使用 n 命令代替 s 来执行整个语句,不进入某个具体函数。但是,还是常常容易后悔,发现某个跳过的地方其实应该跟踪进去的。于是只能重新开始整个调试过程。

有了 DB::Skip 模块,这样的问题就很容易解决了。你可以这样调用它来跳过那些带来调试麻烦的“可靠”模块和函数:

=begin code

    use DB::Skip pkgs => [qr{^(DateTime|Class)}];

=end code

这里的 qr{...} 建立的是一个需要跳过的模块名模式。另外还可以用 subs => [qw( ... )] 建立需要跳过的函数列表。

感谢模块作者 Christian Walde,让我们可以这样完成快速调试:

=begin code

    $ perl -MCalendar::Simple=date_span -lde 'use DB::Skip pkgs => [qr{^(DateTime|Class)}]; date_span(mon => 12, year => 2011,  begin => 1, end => 25)'
   
    Loading DB routines from perl5db.pl version 1.33
    Editor support available.
   
    Enter h or `h h' for help, or `man perldebug' for more help.
   
    DB::DB(-e:1):   use DB::Skip pkgs => [qr{^(DateTime|Class)}]; date_span(mon => 12, year => 2011,  begin => 1, end => 25)
      DB<1> s
    DB::DB(/usr/share/perl5/Calendar/Simple.pm:157):
    ...
    ...
    ...
      DB<1>
    DB::DB(/usr/share/perl5/Calendar/Simple.pm:85):
    85:         $first = DateTime->new(year => $year,
    86:                                month => $mon,
    87:                                day => 1)->day_of_week % 7;
      DB<1> # 现在第 85 行上的单步调用,却不会再进入 DateTime 这个模块了
    DB::DB(/usr/share/perl5/Calendar/Simple.pm:92):
    92:       $first -= $start_day;

=end code

--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

[PerlChina] 2011 CN Perl Advent Day 16: Gearman & TheSchwartz

http://perlchina.github.com/advent.perlchina.org/2011/Gearman_TheSchwartz.html 

=for advent_year 2011

=for advent_day 16

=for advent_title Gearman & TheSchwartz

=for advent_author Fayland Lam

A<http://search.cpan.org/dist/Gearman/|Gearman> 和 M<TheSchwartz> 是著名 Perl 公司 Six Apart (Danga 已被 Six Apart 收购)的作品。

这里我们不详细贴示例代码了,因为 perldoc 和网上搜索的例子都有一大把。

两者都是与 job 相关,但是有各自的优点,可以适合不同的需求。

两者都是比较适合将一些任务派发给别的后台程序来执行。适用的情况比如:

1。有个程序极度占用内存,比如当我们要用 M<Algorithm::Bayesian> 来检查 spam 的时候,spam/ham 数据都是好几百 M,而我们不可能在 n 个 Web 服务器的 n 个进程中全部 slurp 数据文件。

2。有段代码极度占用时间,但是我们不需要考虑其返回值,所以我们不想让 web 服务器在那里等待。

3。有段代码性能极度差劲,你需要用 C 重写。或者某个东西是由其他语言所写,我们想使用它。

等等。

A<http://gearman.org/|gearman> 适合于你需要返回值,或者你需要和其他语言交流。

TheSchwartz 适用于不需要返回值的。

两者构架比较相似。都是有 Server/Client 模式,在一个地方插入对于某个 Worker 的 job, 然后 server 通过函数名字和参数来执行相对应的代码。

当然也有很多不同的地方,TheSchwartz 需要数据库做中转,对于一个 Worker 来说,可以多处插入 job 但为了避免冲突往往只有一个 server 通过 fork 来处理。而 gearman 不需要任何数据库做中转,直接通过 ip servers 来自动分配任务到不同的服务器上,并且自动进行容错处理。

本章只做抛砖引玉,希望诸位需要的时候能有所记忆。谢谢。

--
Fayland Lam // http://www.fayland.org/

--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

2011年12月15日星期四

Re: [PerlChina] what's new in perl 5.14

nice!

在 2011-5-18,上午11:34, woosley. xu. 写道:

这个slides不错
http://blogs.perl.org/users/andrew_shitov/2011/05/whats-new-in-perl-514.html



--
Woosley.Xu




--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

Re: [PerlChina] 2011 CN Perl Advent Day 15: List::MoreUtils

Thanks for extended usage,

with a script named row2col in your path, you will be able to rotate the output 90 degree, by each_arrayref from same module :)

$ lsof -p 1598 | head -3 | row2col
COMMAND bash    bash
PID     1598    1598
USER    jjiang  jjiang
FD      cwd     rtd
TYPE    DIR     DIR
DEVICE  253,2   253,0
SIZE/OFF        12288   4096
NODE    1572865 2
NAME    /home/jjiang    /


2011/12/15 Fayland Lam <fayland@gmail.com>
Yes. it's quite powerful. another example for that is 'part'.

$ seq 1 30 | perl -MList::MoreUtils=part -lne 'push @all, $_; END{ $i; @sets=part {$i++ % 3} @all; print join q( ), q(/usr/bin/md5sum), @$f while $f = shift @sets }'
/usr/bin/md5sum 1 4 7 10 13 16 19 22 25 28
/usr/bin/md5sum 2 5 8 11 14 17 20 23 26 29
/usr/bin/md5sum 3 6 9 12 15 18 21 24 27 30


$ seq 1 30 | perl -MList::MoreUtils=natatime -lne 'push @all, $_; END{ $i=natatime 10, @all; print join q( ), q(/usr/bin/md5sum), @f while @f=$i->() }'
/usr/bin/md5sum 1 2 3 4 5 6 7 8 9 10
/usr/bin/md5sum 11 12 13 14 15 16 17 18 19 20
/usr/bin/md5sum 21 22 23 24 25 26 27 28 29 30

:)

2011/12/15 joe jiang <lamp.purl@gmail.com>
=for advent_year 2011

=for advent_day 15

=for advent_title List::MoreUtils

=for advent_author Joe Jiang

对于大多数人来说,列表的处理是最让人头疼的一件事了,因为循环、变量一类的逻辑非常难以调试。而 List::MoreUtils 则提供了一个简洁的接口,帮助我们进行高效的开发。

这里就用其中一个有用的函数 natatime 作为例子介绍这个常用的模块。

我们常常遇到的一个情况是,通过 Perl 生成 shell 脚本,然后用 shell 来执行。

=begin code

  % perl -le '...' > todo.sh
  % source todo.sh

=end code

如果 todo.sh 里面某一行有许多的参数,比如 md5sum 1.txt 2.txt ... 9999.txt,这就可能导致一个问题,那就是命令行过长:

=begin code

  % md5sum *
  -bash: /usr/bin/md5sum: Argument list too long

=end code

这样的问题,对于喜欢脚本的你来说,迟早会遇到的。natatime 这个有点大舌头的 not_a_time 函数能够帮助你去分批完成任务。

=begin code

  % seq 1 30 | perl -MList::MoreUtils=natatime -lne 'push @all, $_; END{ $i=natatime 10, @all; print join q( ), q(/usr/bin/md5sum), @f while @f=$i->() }'
  /usr/bin/md5sum 1 2 3 4 5 6 7 8 9 10
  /usr/bin/md5sum 11 12 13 14 15 16 17 18 19 20
  /usr/bin/md5sum 21 22 23 24 25 26 27 28 29 30

=end code

这里用 natatime(10, @all) 产生了一个 $i 引用的函数,然后反复调用函数,每次产生十个数据,用来生成最终的命令。

另一个有趣的例子是打印日历,如果你常常需要在命令行显示日历的话,可以用下面的代码实现:

=begin code

  #!/usr/bin/perl -l
  use List::MoreUtils qw(natatime);
  $it=natatime(7, (qw(Sun Mon Tue Wed Thr Fri Sat), (undef)x((8+@{[localtime]}[6]-@{[localtime]}[3]) % 7),map {s{^(@{[localtime]}[3])$}{\1*}; $_} (1..30)));
  print join qq(\t), @days while (@days=$it->())

=end code

运行效果如下:

=begin code

  Sun     Mon     Tue     Wed     Thr     Fri     Sat
                                  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

=end code

需要承认的是,安装 Calendar::Simple 模块也能输出同样功能的脚本 pcal,但是这个脚本毕竟可以在 Windows 下运行。

更多例子可以参考 http://www.effectiveperlprogramming.com/


--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。



--
Fayland Lam // http://www.fayland.org/

--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

Re: [PerlChina] Fwd: Net::Evernote

在 2011年12月15日 下午8:05,HY <think.hy@gmail.com> 写道:
> Good idea! As you mentioned Evernote's API, could you please introduce some
> functions of the API? Thax
>

Well, this is the full API document:
http://www.evernote.com/about/developer/api/evernote-api.htm

The main API protocol is Thrift, all the remote functions are there:
http://www.evernote.com/about/developer/api/ref/

Currently I best wish some people could write a convertor module for
converting the XHTML document to evernote's ENML:
http://www.evernote.com/about/developer/api/evernote-api.htm#_Toc297053072

Thanks.

--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

Re: [PerlChina] Fwd: Net::Evernote

Good idea!  As you mentioned Evernote's API, could you please introduce some functions of the API? Thax

Regards
Thinkhy

在 2011年12月15日 下午12:02,Ken Peng <shorttag@gmail.com>写道:
---------- Forwarded message ----------
From: Ken Peng <shorttag@gmail.com>
Date: 2011/12/15
Subject: Net::Evernote
To: module-authors@perl.org


Hi,

I just created a project on CPAN Net::Evernote, it's the most original
0.01 version.

http://search.cpan.org/~yhpeng/Net-Evernote-0.01/lib/Net/Evernote.pm

Evernote's API is a big collection of functions, I don't have that
much time to implement all of them.

If you anybody has the interest with this project, please let me know,
we could co-work with it.

Thanks.

Ken.

--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。


--
您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。
要向此网上论坛发帖,请发送电子邮件至 perlchina@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至 perlchina+unsubscribe@googlegroups.com。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。