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 访问此网上论坛。

Re: [PerlChina] 2011 CN Perl Advent Day 14: CSV & XLS

from the name, it is: Create a new file in the Excel 2007+ XLSX format.

Thanks.

2011/12/15 Michael Zeng <galaxy2004@gmail.com>
use Excel::Writer::XLSX
是不是支持 xls 2007 格式的
 
 

 
2011/12/14 Fayland Lam <fayland@gmail.com>
http://perlchina.github.com/advent.perlchina.org/2011/CSV_XLS.html 

=for advent_year 2011

=for advent_day 14

=for advent_title CSV & XLS

=for advent_author Fayland Lam

Well, 很多人都喜欢用 Excel or OpenOffice Calc 来打开表格文件。所以您的上司,客户或者朋友,会让你输出 csv 或者 xls 给他们。

有些人喜欢直接用 print join(', ', map { '"' . $_ . '"' } @outs) 来输出成 CSV, 但是这是极度不好的行为,因为 outs 里可能有 " 也可能有 ,

所以个人建议,不管是解析 CSV 或者构造 CSV, 都应该使用标准的 CPAN 模块。如 M<Text::CSV_XS>

=begin code

use Data::Dumper;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 }) or
    die "Cannot use CSV: " . Text::CSV_XS->error_diag();

# read
open(my $fh, "<:encoding(utf8)", "test.csv") or die "test.csv: $!";
while (my $row = $csv->getline($fh)) {
    print Dumper(\$row);
}
$csv->eof or $csv->error_diag();
close($fh);

# read from string
my $line = "myid,filename,size as 9mb";
unless ($csv->parse($line)) {
    die "Failed to parse $line: " . $csv->error_diag() . "\n";
}
my @field = $csv->fields;
print Dumper(\@field);

# print out to file
$csv->print($fh, ['myid', 'filename', 'size as 9mb']); print $fh "\n";
# print out to STDOUT
$csv->print(\*STDOUT, \@rows); print "\n";

=end code

如果有人非要找你要 xls 格式的话,你可以简单的用一下 A<http://cpansearch.perl.org/src/HMBRAND/Text-CSV_XS-0.85/examples/csv2xls|csv2xls> 来转成 xls 给他们。(如果连接失效,可 google "csv2xls cpan")

但是如果他们需要一些特殊的 xls 功能的话,比如一些颜色,大小格式,或者 function 之类的,那我们就得求助于 M<Excel::Writer::XLSX> 了。

该模块由 M<Spreadsheet::WriteExcel> 的作者写的,相对 Spreadsheet::WriteExcel 提供了更好的性能和支持。

模块的使用也是非常简单的。(模块的使用都应该是很简单的,所有复杂的模块都是不合格的模块。:)

我们复制了 SYNOPSIS 过来。

=begin code

use Excel::Writer::XLSX;

# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

# Add a worksheet
$worksheet = $workbook->add_worksheet();

#  Add and define a format
$format = $workbook->add_format();
$format->set_bold();
$format->set_color( 'red' );
$format->set_align( 'center' );

# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write( $row, $col, 'Hi Excel!', $format );
$worksheet->write( 1, $col, 'Hi Excel!' );

# Write a number and a formula using A1 notation
$worksheet->write( 'A3', 1.2345 );
$worksheet->write( 'A4', '=SIN(PI()/4)' );

=end code

2个模块都有非常详细的 EXAMPLES 目录。如果还是不会的话,可以来 PerlChina Group 发帖求助。

谢谢。

--
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 访问此网上论坛。



--
            Yours Sincerely
                    Zeng Hong

--
您收到此邮件是因为您订阅了 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 访问此网上论坛。

Re: [PerlChina] 2011 CN Perl Advent Day 14: CSV & XLS

use Excel::Writer::XLSX
是不是支持 xls 2007 格式的
 
 

 
2011/12/14 Fayland Lam <fayland@gmail.com>
http://perlchina.github.com/advent.perlchina.org/2011/CSV_XLS.html 

=for advent_year 2011

=for advent_day 14

=for advent_title CSV & XLS

=for advent_author Fayland Lam

Well, 很多人都喜欢用 Excel or OpenOffice Calc 来打开表格文件。所以您的上司,客户或者朋友,会让你输出 csv 或者 xls 给他们。

有些人喜欢直接用 print join(', ', map { '"' . $_ . '"' } @outs) 来输出成 CSV, 但是这是极度不好的行为,因为 outs 里可能有 " 也可能有 ,

所以个人建议,不管是解析 CSV 或者构造 CSV, 都应该使用标准的 CPAN 模块。如 M<Text::CSV_XS>

=begin code

use Data::Dumper;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 }) or
    die "Cannot use CSV: " . Text::CSV_XS->error_diag();

# read
open(my $fh, "<:encoding(utf8)", "test.csv") or die "test.csv: $!";
while (my $row = $csv->getline($fh)) {
    print Dumper(\$row);
}
$csv->eof or $csv->error_diag();
close($fh);

# read from string
my $line = "myid,filename,size as 9mb";
unless ($csv->parse($line)) {
    die "Failed to parse $line: " . $csv->error_diag() . "\n";
}
my @field = $csv->fields;
print Dumper(\@field);

# print out to file
$csv->print($fh, ['myid', 'filename', 'size as 9mb']); print $fh "\n";
# print out to STDOUT
$csv->print(\*STDOUT, \@rows); print "\n";

=end code

如果有人非要找你要 xls 格式的话,你可以简单的用一下 A<http://cpansearch.perl.org/src/HMBRAND/Text-CSV_XS-0.85/examples/csv2xls|csv2xls> 来转成 xls 给他们。(如果连接失效,可 google "csv2xls cpan")

但是如果他们需要一些特殊的 xls 功能的话,比如一些颜色,大小格式,或者 function 之类的,那我们就得求助于 M<Excel::Writer::XLSX> 了。

该模块由 M<Spreadsheet::WriteExcel> 的作者写的,相对 Spreadsheet::WriteExcel 提供了更好的性能和支持。

模块的使用也是非常简单的。(模块的使用都应该是很简单的,所有复杂的模块都是不合格的模块。:)

我们复制了 SYNOPSIS 过来。

=begin code

use Excel::Writer::XLSX;

# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

# Add a worksheet
$worksheet = $workbook->add_worksheet();

#  Add and define a format
$format = $workbook->add_format();
$format->set_bold();
$format->set_color( 'red' );
$format->set_align( 'center' );

# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write( $row, $col, 'Hi Excel!', $format );
$worksheet->write( 1, $col, 'Hi Excel!' );

# Write a number and a formula using A1 notation
$worksheet->write( 'A3', 1.2345 );
$worksheet->write( 'A4', '=SIN(PI()/4)' );

=end code

2个模块都有非常详细的 EXAMPLES 目录。如果还是不会的话,可以来 PerlChina Group 发帖求助。

谢谢。

--
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 访问此网上论坛。



--
            Yours Sincerely
                    Zeng Hong

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

2011年12月14日星期三

Re: [PerlChina] 有好的perl调试器或者方法吗

试了下,这个好,很清晰。多谢。

======================================================
常福刚    规划设计中心-系统工程师
 
北京新媒传信科技有限公司
地  址:北京市朝阳区北苑路甲13号院1号楼北辰泰岳大厦16层
邮  编:100107
手  机:15901155677
E-Mail:changfugang@feinno.com
网  址:http://www.feinno.com

From: cnhack TNT
Sent: Wednesday, December 14, 2011 9:19 PM
Subject: Re: [PerlChina] 有好的perl调试器或者方法吗

可以参考一下 Devel::NYTProf 模块: https://metacpan.org/module/Devel::NYTProf

2011/12/14 fugang chang <fire_pot@hotmail.com>
hi,all
 
google到ptkdb工具,正在安装测试,不知道效果咋样~~
大家有没有好的perl调试器或者方法推荐下。
主要是想看下每步执行的时间,确定下优化的问题。谢谢。

--
您收到此邮件是因为您订阅了 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] 2011 CN Perl Advent Day 15: List::MoreUtils

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 访问此网上论坛。

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

=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 访问此网上论坛。

[PerlChina] Fwd: Net::Evernote

---------- 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 访问此网上论坛。

[PerlChina] 2011 CN Perl Advent Day 14: CSV & XLS

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

=for advent_year 2011

=for advent_day 14

=for advent_title CSV & XLS

=for advent_author Fayland Lam

Well, 很多人都喜欢用 Excel or OpenOffice Calc 来打开表格文件。所以您的上司,客户或者朋友,会让你输出 csv 或者 xls 给他们。

有些人喜欢直接用 print join(', ', map { '"' . $_ . '"' } @outs) 来输出成 CSV, 但是这是极度不好的行为,因为 outs 里可能有 " 也可能有 ,

所以个人建议,不管是解析 CSV 或者构造 CSV, 都应该使用标准的 CPAN 模块。如 M<Text::CSV_XS>

=begin code

use Data::Dumper;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 }) or
    die "Cannot use CSV: " . Text::CSV_XS->error_diag();

# read
open(my $fh, "<:encoding(utf8)", "test.csv") or die "test.csv: $!";
while (my $row = $csv->getline($fh)) {
    print Dumper(\$row);
}
$csv->eof or $csv->error_diag();
close($fh);

# read from string
my $line = "myid,filename,size as 9mb";
unless ($csv->parse($line)) {
    die "Failed to parse $line: " . $csv->error_diag() . "\n";
}
my @field = $csv->fields;
print Dumper(\@field);

# print out to file
$csv->print($fh, ['myid', 'filename', 'size as 9mb']); print $fh "\n";
# print out to STDOUT
$csv->print(\*STDOUT, \@rows); print "\n";

=end code

如果有人非要找你要 xls 格式的话,你可以简单的用一下 A<http://cpansearch.perl.org/src/HMBRAND/Text-CSV_XS-0.85/examples/csv2xls|csv2xls> 来转成 xls 给他们。(如果连接失效,可 google "csv2xls cpan")

但是如果他们需要一些特殊的 xls 功能的话,比如一些颜色,大小格式,或者 function 之类的,那我们就得求助于 M<Excel::Writer::XLSX> 了。

该模块由 M<Spreadsheet::WriteExcel> 的作者写的,相对 Spreadsheet::WriteExcel 提供了更好的性能和支持。

模块的使用也是非常简单的。(模块的使用都应该是很简单的,所有复杂的模块都是不合格的模块。:)

我们复制了 SYNOPSIS 过来。

=begin code

use Excel::Writer::XLSX;

# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

# Add a worksheet
$worksheet = $workbook->add_worksheet();

#  Add and define a format
$format = $workbook->add_format();
$format->set_bold();
$format->set_color( 'red' );
$format->set_align( 'center' );

# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write( $row, $col, 'Hi Excel!', $format );
$worksheet->write( 1, $col, 'Hi Excel!' );

# Write a number and a formula using A1 notation
$worksheet->write( 'A3', 1.2345 );
$worksheet->write( 'A4', '=SIN(PI()/4)' );

=end code

2个模块都有非常详细的 EXAMPLES 目录。如果还是不会的话,可以来 PerlChina Group 发帖求助。

谢谢。

--
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 访问此网上论坛。

Re: [PerlChina] 有好的perl调试器或者方法吗

可以参考一下 Devel::NYTProf 模块: https://metacpan.org/module/Devel::NYTProf

2011/12/14 fugang chang <fire_pot@hotmail.com>
hi,all
 
google到ptkdb工具,正在安装测试,不知道效果咋样~~
大家有没有好的perl调试器或者方法推荐下。
主要是想看下每步执行的时间,确定下优化的问题。谢谢。

--
您收到此邮件是因为您订阅了 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] 有好的perl调试器或者方法吗

hi,all
 
google到ptkdb工具,正在安装测试,不知道效果咋样~~
大家有没有好的perl调试器或者方法推荐下。
主要是想看下每步执行的时间,确定下优化的问题。谢谢。

[PerlChina] Getopt::Euclid

大家跑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 访问此网上论坛。

2011年12月13日星期二

Re: [PerlChina] 2011 CN Perl Advent Day 13: Gmail

厉害厉害,gmail 都用上了


 
2011/12/13 Fayland Lam <fayland@gmail.com>
http://perlchina.github.com/advent.perlchina.org/2011/Gmail.html 

=for advent_year 2011

=for advent_day 13

=for advent_title Gmail

=for advent_author Fayland Lam

Gmail 挺不错的。本章主要介绍如何使用 IMAP 来收取邮件和如何在 TLS 上发送邮件。

Gmail 支持 POP3, 但是个人更喜欢用 IMAP,因为可以只查询未读邮件,和标记为已读,无须将其删除。还有它还能检查非 INBOX 的邮件。

IMAP 一般使用 M<Mail::IMAPClient>, 使用例子如下:

=begin code

use Mail::IMAPClient;
use IO::Socket::SSL;

# Connect to IMAP server
my $imap = Mail::IMAPClient->new(
  Server   => 'imap.gmail.com',
  User     => 'myusername@gmail.com', # or App mail
  Password => 'mysecretpass',
  Port     => 993,
  Ssl      =>  1,
) or die "Cannot connect through IMAPClient: $!";

$imap->select("INBOX");
$imap->Peek(1); # we'll use see to mark as read
my @unseen = $imap->unseen or print "No unseen messages in inbox\n";
foreach my $msg_id (@unseen) {
    my $msg_from = $imap->get_header($msg_id, "From");
    my $msg_subject = $imap->get_header($msg_id, "Subject");
    my $string = $imap->body_string($msg_id)
        or die "Could not body_string: ", $imap->LastError;
    print "We get $msg_subject from $msg_from: $string\n";
    
    $imap->see($msg_id); # mark as read if needed
}

# Say so long
$imap->logout();

=end code

邮件的用途比较广泛,比如你在其他地方或者某些人会经常给你发送一些特定格式的邮件,您可以写个脚本来处理这些。

另外我们也经常需要发送邮件,如果要大批量发送的话,sendmail, Qmail 或者 Postfix 等是一个不错的选择,但是如果仅仅是需要偶尔发送一些邮件,还是可以使用 Gmail 的 SMTP 服务器的。这里我们用 M<Email::Sender::Simple> 和 M<Email::Sender::Transport::SMTP::TLS> 来发送。

=begin pre

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP::TLS;
use Try::Tiny;

my $transport = Email::Sender::Transport::SMTP::TLS->new(
    host => 'smtp.gmail.com',
    port => 587,
    username => 'myusername@gmail.com',
    password => 'mysecretpass',
);


use Email::Simple::Creator; # 需要发送附件的话,可以试试 MIME::Lite 到最后 my $message = $msg->as_string; 即可
my $message = Email::Simple->create(
    header => [
        From    => 'myusername@gmail.com',
        To      => 'mylove@mail.com',
        Subject => 'Hi Baby',
    ],
    body => 'I Love you!',
);

try {
    sendmail($message, { transport => $transport });
} catch {
    die "Error sending email: $_";
};

=end pre

更多更详细的资料,请参阅各自的 perldoc

谢谢。


--
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 访问此网上论坛。



--
            Yours Sincerely
                    Zeng Hong

--
您收到此邮件是因为您订阅了 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 13: Gmail

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

=for advent_year 2011

=for advent_day 13

=for advent_title Gmail

=for advent_author Fayland Lam

Gmail 挺不错的。本章主要介绍如何使用 IMAP 来收取邮件和如何在 TLS 上发送邮件。

Gmail 支持 POP3, 但是个人更喜欢用 IMAP,因为可以只查询未读邮件,和标记为已读,无须将其删除。还有它还能检查非 INBOX 的邮件。

IMAP 一般使用 M<Mail::IMAPClient>, 使用例子如下:

=begin code

use Mail::IMAPClient;
use IO::Socket::SSL;

# Connect to IMAP server
my $imap = Mail::IMAPClient->new(
  Server   => 'imap.gmail.com',
  User     => 'myusername@gmail.com', # or App mail
  Password => 'mysecretpass',
  Port     => 993,
  Ssl      =>  1,
) or die "Cannot connect through IMAPClient: $!";

$imap->select("INBOX");
$imap->Peek(1); # we'll use see to mark as read
my @unseen = $imap->unseen or print "No unseen messages in inbox\n";
foreach my $msg_id (@unseen) {
    my $msg_from = $imap->get_header($msg_id, "From");
    my $msg_subject = $imap->get_header($msg_id, "Subject");
    my $string = $imap->body_string($msg_id)
        or die "Could not body_string: ", $imap->LastError;
    print "We get $msg_subject from $msg_from: $string\n";
    
    $imap->see($msg_id); # mark as read if needed
}

# Say so long
$imap->logout();

=end code

邮件的用途比较广泛,比如你在其他地方或者某些人会经常给你发送一些特定格式的邮件,您可以写个脚本来处理这些。

另外我们也经常需要发送邮件,如果要大批量发送的话,sendmail, Qmail 或者 Postfix 等是一个不错的选择,但是如果仅仅是需要偶尔发送一些邮件,还是可以使用 Gmail 的 SMTP 服务器的。这里我们用 M<Email::Sender::Simple> 和 M<Email::Sender::Transport::SMTP::TLS> 来发送。

=begin pre

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP::TLS;
use Try::Tiny;

my $transport = Email::Sender::Transport::SMTP::TLS->new(
    host => 'smtp.gmail.com',
    port => 587,
    username => 'myusername@gmail.com',
    password => 'mysecretpass',
);


use Email::Simple::Creator; # 需要发送附件的话,可以试试 MIME::Lite 到最后 my $message = $msg->as_string; 即可
my $message = Email::Simple->create(
    header => [
        From    => 'myusername@gmail.com',
        To      => 'mylove@mail.com',
        Subject => 'Hi Baby',
    ],
    body => 'I Love you!',
);

try {
    sendmail($message, { transport => $transport });
} catch {
    die "Error sending email: $_";
};

=end pre

更多更详细的资料,请参阅各自的 perldoc

谢谢。


--
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月12日星期一

[PerlChina] AnyEvent timer + AnyEvent-HTTP 出现的memleak问题

Hi all,

貌似最近AnyEvent挺火,以前也尝试过,实在用不怪,
近几天又拿来用,发现个问题。

如下例,
AnyEvent timer 回调中设置了一个AnyEvent::HTTP的http_get,
并且指定一个代理,这段代码运行一个小时内存使用了100M,
若去掉代理,内存消耗就正常。

若改下代码,http_get放在一个循环中,则也未见memleak出现。

搞不清这个memleak 是由timer引起的,还是AnyEvent-HTTP引起的。
是bug? 还是我没用好?

帮忙分析下,先谢。

Code:
##################
use AnyEvent;
use AnyEvent::HTTP;

local $| = 1;

my $cv = AnyEvent->condvar;
my $url = 'http://www.google.com';

my $w1 = AnyEvent->timer(
after => 1,
interval => 1,
cb => sub { get($url) },
);

$cv->recv;

sub get {
my $url = shift;

AnyEvent::HTTP::http_get ($url, proxy => ['127.0.0.1', 8080], sub {
my ($body, $hdr) = @_;
print $hdr->{Status}, "\n";
});
}
####################

--
您收到此邮件是因为您订阅了 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 12: 最简单的 Git 服务器

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

=for advent_year 2011

=for advent_day 12

=for advent_title 最简单的 Git 服务器

=for advent_author 扶凯

=head1 简单介绍

目前大家的开发平台都从 svn 转成了 git 来管理应用了。说明老大们的影响力还真是不小啊. 近来查了一下资料配置 Apache 加 dav 来管理是相当的麻烦。现在用习惯了 M<Plack> 的方案来做 Web 的应用,太方便了。所以都感觉配置 Apache 之类是很麻烦的一个事情。

现在 Plack 很火,今天就介绍怎么使用 Plack 来实现 Git 服务器,在这,我们使用的是 M<Plack::App::DAV> 这个应用.

=head1 配置 Git 的客户端

因为 Git 的服务器在 Linux 中是使用的 Curl ,所以一定要先配置 Curl 的默认的用户和密码(注: 此设定不可略过).修改这个的默认的配置文件 ~/.netrc .

    machine example.com
    login USERNAME
    password PASSWORD

可以用这个来测试一下下面的 Git 的库认证(怎么样建一个 HTTP 的 Git 的库,请 Google .)

    curl --netrc --location http://git.example.com/project_name.git/HEAD 

会看到如下的输出 

    ref: refs/heads/master 

=head1 建立 DAV 的服务器
    
在 Plack 中有很多应用,都象下面这样的单行,比如我常用的静态文件服务器,就是个使用 Plack 的单行的 Perl 。这个 DAV 的服务器也能这样。
但只是 DAV 的服务器在 Git  中并行不通,因为一定需要用户认证。所面只是一个建立 DAV 服务器的演示.

    plackup -MPlack::App::DAV -e 'Plack::App::DAV->new->to_app'
    

=head2 最简单的 Git 服务器

最简单的 Git 的服务器。这个一定需要用户和密码才行。因为 Git 多人读取文件需要使用了一个外部锁认证,在这会根据用户名来做文件共享锁的识别。

    #!/usr/bin/perl
    use strict;
    use Plack::Builder;
    use Plack::App::DAV;
    
    my $gitPath = '/data/dev/git'; 
    my $app      = Plack::App::DAV->new(root => $gitPath )->to_app;
    
    builder {
        enable "Auth::Basic", authenticator => sub {
            my($username, $password) = @_; 
            return $username eq 'admin' && $password eq 'passwd';
        };  
        $app;
    };

启动这个 Git 的服务器

    plackup git.psgi

=head2 支持只读用户和读写用户的 Git 服务器 

这个需要使用使用 Plack 的中间件 Options 来管理读写的 HTTP 的方法.另外我还使用了一个 Junction 的模块,超级好用,也值得推荐.

    #!/usr/bin/perl
    use strict;
    use Plack::Builder;
    use Plack::App::DAV;
    use Perl6::Junction qw(any);
    
    
    my %userList = (
        test1 => 'pass1',
        test2 => 'pass2',
        );
    
    my @readOnly = qw( test1 );
    my $gitPath = '/data/dev/git'; 
    
    my $app = Plack::App::DAV->new(root => $gitPath )->to_app;
     
    builder {
        enable "Auth::Basic", authenticator => sub {
            my($user, $pass) = @_;
            return exists $userList{$user} and $pass eq $userList{$user};
        };
        enable 'Options', allowed => [qw/GET PUT POST DELETE  HEAD PROPPATCH PROPFIND MKCOL COPY MOVE LOCK UNLOCK/];
        enable_if { any(@readOnly) eq  $_[0]->{REMOTE_USER}  } 
            "Options", allowed => [qw/GET HEAD/];
        $app;
    };

启动这个 Git 的服务器

    plackup git.psgi

我们还可以使用其它的方法来管理读写用户,比如通过路径。这时我们可以使用 Plack::App::URLMap 的模块。


--
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 访问此网上论坛。

Re: [PerlChina] Re: db-layout-graphviz

Thanks for that.  http://perlchina.github.com/advent.perlchina.org/2011/db-layout-graphviz.html 

2011/12/12 joe jiang <lamp.purl@gmail.com>
Sorry a mistake at copy paste, please replace DB<1> with this:

  DB<1> use GraphViz::DBI; sub GraphViz::DBI::is_foreign_key { return qq(`employees`.`$_->{REFERENCED_TABLE_NAME}`) for @{$_[0]->{dbh}->selectall_arrayref(q{select REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from information_schema.key_column_usage where REFERENCED_COLUMN_NAME=? and COLUMN_NAME=? and TABLE_SCHEMA=? and TABLE_NAME=?}, {Slice => {}}, $_[2], $_[2], $_[1]=~m{(\w+)\W+(\w+)})} }


2011/12/12 joe jiang <lamp.purl@gmail.com>
=for advent_day 11

=for advent_title GraphViz::DBI::mysql

=for advent_author Joe Jiang

其实这还不是一个真正可用的模块,这里的名字只是预备将来发布的,所以请各位看官大家
手下留情,先不要注册掉它。

GraphViz::DBI 是一个基于 GraphViz 的模块,目标是用来进行数据库 ER 图的绘制,可以
输出的格式非常多,这里用 PNG 格式来举例。

要使用这个模块,首先要有一个可用的数据库 DBI 连接。然后要覆盖这个模块的外键参考依赖子程序,这样才能从特定数据库获取元数据。最后,还要用这个修正过的模块来输出图
片。

这里的 MySQL 数据库使用了样本数据库 employees,可以在网上下载 https://launchpad.net/test-db/employees-db-1。这里假定样本数据库已经创建并导入,并且用 DBI 登录 MySQL 的问题已经解决。

第一步,启动 Perl 调试器(perl -de 0),载入并修正 GraphViz::DBI:

=begin code

  DB<1> use GraphViz::DBI; sub GraphViz::DBI::is_foreign_key { return qq(`employees`.`@{[$_[0]->{dbh}->selectall_arrayref(q{select REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from information_schema.key_column_usage where REFERENCED_COLUMN_NAME=? and COLUMN_NAME=? and TABLE_SCHEMA=? and TABLE_NAME=?}, {Slice => {}}, $_[2], $_[2], $_[1]=~m{(\w+)\W+(\w+)})->[0]->{REFERENCED_TABLE_NAME}]}`) }

=end code

这里通过查询了 MySQL 的元数据 information_schema.key_column_usage,从其中查找某个表的字段是否有参考的主表,若查询结果不为空的话,就返回表名作为结果,否则返回空


第二步,创建数据库连接,这里要求用户能够不用密码登录,且获得了查询 information_schema 的权限:

=begin code

  DB<2> use DBI; $d=DBI->connect(q(DBI:mysql:database=employees), q(), q())

=end code

第三步,命名临时文件,并正式驱动模块生成图片:

=begin code

  DB<3> x open F, q(>), q(tmp.1sKZjd1d4L.png); print F GraphViz::DBI->new($d)->graph_tables->as_png

=end code

欢迎尝试其他数据库,效果应该还不错 :)


--
您收到此邮件是因为您订阅了 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 访问此网上论坛。

2011年12月11日星期日

[PerlChina] Re: db-layout-graphviz

Sorry a mistake at copy paste, please replace DB<1> with this:

  DB<1> use GraphViz::DBI; sub GraphViz::DBI::is_foreign_key { return qq(`employees`.`$_->{REFERENCED_TABLE_NAME}`) for @{$_[0]->{dbh}->selectall_arrayref(q{select REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from information_schema.key_column_usage where REFERENCED_COLUMN_NAME=? and COLUMN_NAME=? and TABLE_SCHEMA=? and TABLE_NAME=?}, {Slice => {}}, $_[2], $_[2], $_[1]=~m{(\w+)\W+(\w+)})} }

2011/12/12 joe jiang <lamp.purl@gmail.com>
=for advent_day 11

=for advent_title GraphViz::DBI::mysql

=for advent_author Joe Jiang

其实这还不是一个真正可用的模块,这里的名字只是预备将来发布的,所以请各位看官大家
手下留情,先不要注册掉它。

GraphViz::DBI 是一个基于 GraphViz 的模块,目标是用来进行数据库 ER 图的绘制,可以
输出的格式非常多,这里用 PNG 格式来举例。

要使用这个模块,首先要有一个可用的数据库 DBI 连接。然后要覆盖这个模块的外键参考依赖子程序,这样才能从特定数据库获取元数据。最后,还要用这个修正过的模块来输出图
片。

这里的 MySQL 数据库使用了样本数据库 employees,可以在网上下载 https://launchpad.net/test-db/employees-db-1。这里假定样本数据库已经创建并导入,并且用 DBI 登录 MySQL 的问题已经解决。

第一步,启动 Perl 调试器(perl -de 0),载入并修正 GraphViz::DBI:

=begin code

  DB<1> use GraphViz::DBI; sub GraphViz::DBI::is_foreign_key { return qq(`employees`.`@{[$_[0]->{dbh}->selectall_arrayref(q{select REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from information_schema.key_column_usage where REFERENCED_COLUMN_NAME=? and COLUMN_NAME=? and TABLE_SCHEMA=? and TABLE_NAME=?}, {Slice => {}}, $_[2], $_[2], $_[1]=~m{(\w+)\W+(\w+)})->[0]->{REFERENCED_TABLE_NAME}]}`) }

=end code

这里通过查询了 MySQL 的元数据 information_schema.key_column_usage,从其中查找某个表的字段是否有参考的主表,若查询结果不为空的话,就返回表名作为结果,否则返回空


第二步,创建数据库连接,这里要求用户能够不用密码登录,且获得了查询 information_schema 的权限:

=begin code

  DB<2> use DBI; $d=DBI->connect(q(DBI:mysql:database=employees), q(), q())

=end code

第三步,命名临时文件,并正式驱动模块生成图片:

=begin code

  DB<3> x open F, q(>), q(tmp.1sKZjd1d4L.png); print F GraphViz::DBI->new($d)->graph_tables->as_png

=end code

欢迎尝试其他数据库,效果应该还不错 :)


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

[PerlChina] db-layout-graphviz

=for advent_day 11

=for advent_title GraphViz::DBI::mysql

=for advent_author Joe Jiang

其实这还不是一个真正可用的模块,这里的名字只是预备将来发布的,所以请各位看官大家
手下留情,先不要注册掉它。

GraphViz::DBI 是一个基于 GraphViz 的模块,目标是用来进行数据库 ER 图的绘制,可以
输出的格式非常多,这里用 PNG 格式来举例。

要使用这个模块,首先要有一个可用的数据库 DBI 连接。然后要覆盖这个模块的外键参考依赖子程序,这样才能从特定数据库获取元数据。最后,还要用这个修正过的模块来输出图
片。

这里的 MySQL 数据库使用了样本数据库 employees,可以在网上下载 https://launchpad.net/test-db/employees-db-1。这里假定样本数据库已经创建并导入,并且用 DBI 登录 MySQL 的问题已经解决。

第一步,启动 Perl 调试器(perl -de 0),载入并修正 GraphViz::DBI:

=begin code

  DB<1> use GraphViz::DBI; sub GraphViz::DBI::is_foreign_key { return qq(`employees`.`@{[$_[0]->{dbh}->selectall_arrayref(q{select REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME from information_schema.key_column_usage where REFERENCED_COLUMN_NAME=? and COLUMN_NAME=? and TABLE_SCHEMA=? and TABLE_NAME=?}, {Slice => {}}, $_[2], $_[2], $_[1]=~m{(\w+)\W+(\w+)})->[0]->{REFERENCED_TABLE_NAME}]}`) }

=end code

这里通过查询了 MySQL 的元数据 information_schema.key_column_usage,从其中查找某个表的字段是否有参考的主表,若查询结果不为空的话,就返回表名作为结果,否则返回空


第二步,创建数据库连接,这里要求用户能够不用密码登录,且获得了查询 information_schema 的权限:

=begin code

  DB<2> use DBI; $d=DBI->connect(q(DBI:mysql:database=employees), q(), q())

=end code

第三步,命名临时文件,并正式驱动模块生成图片:

=begin code

  DB<3> x open F, q(>), q(tmp.1sKZjd1d4L.png); print F GraphViz::DBI->new($d)->graph_tables->as_png

=end code

欢迎尝试其他数据库,效果应该还不错 :)

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