sunnavy's homepage http://sunnavy.net sunnavy@gmail.com Wed, 13 Jul 2011 07:39:59 GMT : my $profile = { : name => 'sunnavy', : email => 'sunnavy@gmail.com', : location => 'hangzhou', : gender => 'male', : age => (localtime)[5] - 84, : favorite_language => 'Perl', : interests => : [qw/nature science programming literature music football/], : map { $_ . '_id ' => 'sunnavy' } qw/cpan github twitter flickr/, : }; static/e8/5189ec797b11e0bd053c2b40bd1a76/sunnavy.png sunnavy's homepage http://sunnavy.net 80 80 : my $profile = { : name => 'sunnavy', : email => 'sunnavy@gmail.com', : location => 'hangzhou', : gender => 'male', : age => (localtime)[5] - 84, : favorite_language => 'Perl', : interests => : [qw/nature science programming literature music football/], : map { $_ . '_id ' => 'sunnavy' } qw/cpan github twitter flickr/, : }; No wait for email sending via slow SMTP http://sunnavy.net/entry/4fd11512ad2311e08decd85f05a9445d "sunnavy" <sunnavy@gmail.com> article, article, perl, email, mutt, smtp http://sunnavy.net/entry/4fd11512ad2311e08decd85f05a9445d Wed, 13 Jul 2011 07:39:59 GMT <p>How many seconds do you usually wait after typing 'y' in Mutt to send an email? I wait for 10 seconds or so normally, sigh.</p> <p>So I chose to speed this up by sending emails to a directory first via <a href="/static/4f/d11512ad2311e08decd85f05a9445d/sendmail-todir">sendmail-todir</a>, then doing the real sending via <a href="/static/4f/d11512ad2311e08decd85f05a9445d/sendmail-fromdir">sendmail-fromdir</a>.</p> <p>First tell mutt to use sendmail-todir</p> <pre> # in .muttrc set sendmail="sendmail-todir --dir /home/sunnavy/.mail_to_send" </pre> <p>sendmail-fromdir is a bit complex, so we need a config file:</p> <pre> dir: /home/sunnavy/.mail_to_send smtp_options: sasl_username: 'alice@example.com' sasl_password: '*******' host: 'smtp.example.com' ssl: 1 port: 465 refresh: 3 # loop forever and wait 3 seconds after each loop; 0 means no loop on_error: /path/to/notify/cmd # the only argument is the error msg die_on_error: 1 # die on sending error </pre> <pre class="shell">sendmail-fromdir --config /path/to/config </pre> <p>With this, I don't need to wait any more ;)</p> Feed RT emails via IMAP http://sunnavy.net/entry/a7a4019cac8711e0bc4654bf05a9445d "sunnavy" <sunnavy@gmail.com> article, article, http://sunnavy.net/entry/a7a4019cac8711e0bc4654bf05a9445d Tue, 12 Jul 2011 13:05:45 GMT <p><a href="http://bestpractical.com/rt/">RT</a> has a great email interface so users can create/update tickets with their favorite email clients, which requires admin to configure email server somehow to feed emails from users back to RT.</p> <p>But you may use remote email servers like <a href="http://mail.google.com">gmail</a> or alike, in which case you can't do that kind of configuration, this is the situation I'm gonna talk about.</p> <p>Here comes script <a href="/static/a7/a4019cac8711e0bc4654bf05a9445d/rt-imap">rt-imap</a>, which makes use of <a href="http://search.cpan.org/~plobbes/Mail-IMAPClient-3.28/lib/Mail/IMAPClient.pod">Mail::IMAPClient</a> and rt-mailgate so you can run it anywhere as long as you have perl and modules rt-imap and rt-mailgate require.</p> <p>Let's take a simple example (assuming email server supports email aliases):</p> <p>We use rt@example.com to correspond to and rt+comment@example.com to comment to.</p> <p>Here is what the config file(default is ~/.rtimaprc) looks like:</p> <pre> imap_options: # args for Mail::IMAPClient->new Server: 'imap.example.com' User: 'rt@example.com' Password: '******' Port: 993 Ssl: 1 Uid: 1 Keepalive: 1 Peek: 1 IgnoreSizeErrors: 1 rt_map: 'rt@example.com': queue: 'General' action: 'correspond' 'rt+comment@example.com': queue: 'General' action: 'comment' rt_url: 'http://rt.example.com' rt_mailgate: '/path/to/rt-mailgate' refresh: 3 # loop forever and sleep 3 seconds after each loop; 0 means no loop </pre> <p>after creating the config file, you can run rt-imap:</p> <pre class="shell">rt-imap -c /path/to/config </pre> <p>Good luck!</p> make wireless work by updating r8192e_pci.ko http://sunnavy.net/entry/5dd29546df1511dfa60fa23b96352ba3 "sunnavy" <sunnavy@gmail.com> article, article, ubuntu, wireless, rtl8192e http://sunnavy.net/entry/5dd29546df1511dfa60fa23b96352ba3 Sun, 24 Oct 2010 02:21:10 GMT <p>I updated to ubuntu 10.10 64bit recently, and found wireless didn't work any more like it does in 32bit.</p> <p>after updating r8192e_pci.ko, it works again!</p> <p>It's sad that I couldn't find updated driver in realtek's official page, luckily I found <a href="http://sites.google.com/site/johnfoshay/rtl8192e_linux_2.6.0014.0401.2010.tar.gz?attredirects=0&amp;d=1">one</a> via google (thanks, johnfoshay!)</p> <p>I also attached it here just in case.</p> <p>here is what I did:</p> <ul> <li>tar xvfz rtl8192e_linux_2.6.0014.0401.2010.tar.gz</li> <li>cd rtl8192e_linux_2.6.0014.0401.2010</li> <li>make</li> <li>sudo cp HAL/rtl8192/r8192e_pci.ko /lib/modules/`uname -r`/kernel/drivers/staging/rtl8192e/</li> <li>reboot</li> </ul> <p>Good luck to you too!</p> 拼音的声调表示 http://sunnavy.net/entry/940990b4bda111dfae38bb247dbf46fb "sunnavy" <sunnavy@gmail.com> article, article, Perl5, Programming, CPAN http://sunnavy.net/entry/940990b4bda111dfae38bb247dbf46fb Sat, 11 Sep 2010 12:39:11 GMT <p>前几天写文章的时候突然发现不能很方便地输入拼音的声调,所以写了个<a href="http://search.cpan.org/search?mode=module;query=Lingua%3A%3AHan%3A%3ACanonicalPinYin">小模块</a>来干这件事。</p> <p>用法很简单,下面是一个输出拼音的例子:</p> <pre class="prettyprint"> use encoding 'utf8'; use Lingua::Han::CanonicalPinYin 'canonicalize_pinyin'; print canonicalize_pinyin( $_ ), "\n" for @ARGV; </pre> <p>如果输入"wen1", 就会输出"wēn",同理还可以输出"wén"(wen2),"wěn"(wen3)和"wèn"(wen4)</p> datetime input with jquery.ui.datepicker and jquery.timepickr http://sunnavy.net/entry/797f97829a0811df9fb2620a25066f7c "sunnavy" <sunnavy@gmail.com> article, article, javascript, web http://sunnavy.net/entry/797f97829a0811df9fb2620a25066f7c Wed, 28 Jul 2010 05:25:03 GMT <p><a href="http://jqueryui.com">jQueryUI</a> has a great <a href="http://jqueryui.com/demos/datepicker/">date picker</a>, and there is a great <a href="http://haineault.com/media/jquery/ui-timepickr/page/">time picker</a> based on jQueryUI.</p> <p>Sadly, I haven't seen a great datetime picker yet, where the UI is simple and people don't need to click a dozen of times.</p> <p>I'm not gonna to write a brand new one( I don't have that much of time, and I'm not a js master yet either ;), so I chose to combine the 2 js lib above, and seems that's good enough for me at least for now.</p> <p>Demo is <a href="/static/79/7f97829a0811df9fb2620a25066f7c//datetime-input.html">Here</a></p> Perl6之九 - cross and zip operators http://sunnavy.net/entry/f56463d4987b11dfb5970bf5dda2278e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/f56463d4987b11dfb5970bf5dda2278e Mon, 26 Jul 2010 06:06:44 GMT <p>二者都是元运算符(metaoperator),参数均为列表。</p> <p>corss的符号是X,后接一个二元运算符,操作类似于集合中的笛卡尔积。</p> <p>zip的符号是Z,后接一个二元运算符,与corss的区别见例子</p> <p>e.g.</p> <pre class="prettyprint"> 1,2 X+ 3,4 # (1+3, 1+4, 2+3, 2+4), i.e. 4, 5, 5, 6 <a defang_b> X~ <!--defang_c defang_d--> # 'ac', 'ad', 'bc', 'bd' <!-- close inline tag into block --></a></pre> <pre class="prettyprint"><!-- reopen inline tag after block --><a defang_b> 1,2 Z+ 3,4 # (1+3, 2+4), i.e. 4, 6 <a defang_b> Z~ <!--defang_c defang_d--> # 'ac', 'bd' <!-- close inline tag into block --></a><!-- close inline tag into block --></a></pre><!-- reopen inline tag after block --><a defang_b><!-- reopen inline tag after block --><a defang_b> <!-- close unclosed tag --></a><!-- close unclosed tag --></a> Perl6之二 - sigil http://sunnavy.net/entry/722cc5b697a811dfba60db0bdea2278e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/722cc5b697a811dfba60db0bdea2278e Sun, 25 Jul 2010 04:52:39 GMT <p>与Perl5很不相同的是,sigil不会变化,scalar永远是$,array永远是@,hash永远是%。</p> <p>e.g.</p> <p>Perl6:</p> <pre class="prettyprint"> my $scalar = [1,2,3]; $scalar[0]; # return 1 my @array = (1,2,3); @array[0]; # return 1 my %hash = ( one => 1, two => 2 ); %hash<!--defang_one-->; # return 1, equal to %hash{'one'} </pre> <p>等价的Perl5:</p> <pre class="prettyprint"> my $scalar = [1,2,3]; ${$scalar}[0]; # return 1 my @array = (1,2,3); $array[0]; # return 1 my %hash = ( one => 1, two => 2 ); $hash{one}; # return 1 </pre> <p>同时,sigil的意思也扩展了:</p> <p>@x可以绑定任何实现了Positional role的对象,%x对应Associative,%&x对应Callable,::x对应Abstraction。$x没有对应的role,所以可以绑定任何对象.</p> <p>另外,Perl6还增加了secondary sigil (twigil),以用来指代不同作用域的变量:</p> <pre class="prettyprint"> $.foo # object attribute public accessor $^foo # self-declared formal positional parameter $:foo # self-declared formal named parameter $*foo # dynamically overridable global variable $?foo # compiler hint variable $=foo # Pod variable $\<!--defang_foo\--> # match variable, short for $/{'foo'} $!foo # object attribute private storage $~foo # the foo sublanguage seen by the parser at this lexical spot </pre> Perl6之一 - 注释 http://sunnavy.net/entry/5ad13ebe978111df80f6c403dfa2278e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/5ad13ebe978111df80f6c403dfa2278e Sun, 25 Jul 2010 00:12:54 GMT <p>除了在引号里,"#"总是意味着注释,所以不能再像Perl5一样用作分隔符:</p> <pre class="prettyprint"> qq#ok# # wrong in Perl6 </pre> <p>Perl6增加了内嵌注释:</p> <p>'#`'后跟一对括号</p> <p>e.g.</p> <pre class="prettyprint"> say #`( I'm a comment ) "howdy world" say #`(( I'm a comment too )) "howdy world" say #`[ I am another comment] "howdy world" say #`{{ multiple line comments }} "howdy world" </pre> App::moduleshere http://sunnavy.net/entry/db6921dc4f1e11df818cc5127ddae767 "sunnavy" <sunnavy@gmail.com> article, article, Perl5, Programming, CPAN http://sunnavy.net/entry/db6921dc4f1e11df818cc5127ddae767 Fri, 23 Apr 2010 21:26:25 GMT <p>used to copy module files(.pm) in @INC to current working directory or somewhere.</p> <p>it's on <a href="http://search.cpan.org/search?mode=module;query=app%3A%3Amoduleshere">CPAN</a></p> Perl第二纪 http://sunnavy.net/entry/2c97ed4a4d5311dfab956ff3a9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Programming, Perl5 http://sunnavy.net/entry/2c97ed4a4d5311dfab956ff3a9a5093e Wed, 21 Apr 2010 14:35:52 GMT <p>昨天<a href="http://blogs.perl.org/users/jt_smith/">JT Smith</a>发表了一篇博文:<a href="http://blogs.perl.org/users/jt_smith/2010/04/the-second-age-of-perl.html "> the second age of perl</a>,宣称Perl 5的第二纪已经开始, 如下是此文的翻译.</p> <h4>Perl的第二纪</h4> <p>多少年了,人们一直在说,Perl已经死了或者说正在死。<a href="http://www.oreillynet.com/onlamp/blog/2007/08/perl_is_dead_long_live_perl.html "> 我甚至也这么说过</a>,虽然我的意思和字面上的并不一样。</p> <p>今天我自作主张在此宣布,我们已经在Perl的第二纪了,不过这和Perl 6没有关系!</p> <p>这并不是说我不喜欢Perl 6/Rakudo,实际上我很喜欢,只是现在我还并不依赖它工作。Perl 5是我们现在所依赖的,我想这种情况还将持续好些年。</p> <p>我们进入第二纪其实已经有段日子了,只是没有意识到罢了。Tatsuhiko Miyagawa, Ovid, Brian Foy, Stevan Little, MST, DaveRolsky,还有好些巨人(当然,也不能忽略我们这些小人物的贡献,我是说整个社区),他们的工作(以Class::MOP, Perl::Critic, DBIx::Class, DateTime, and Moose作为开端)构成了第二纪的基础。真正带领我们进入第二纪的是在这些基础之上创造的新东西,比如Padre, Plack, MooseX,Mojolicious和Dancer,还有cpanminus,Starman和Dist::Zilla,更别说Strawberry Perl了。</p> <p>我坐在这里写这篇文章的时候,脑袋里充满了我能列出来的模块和程序,但是第二纪并不只是代码。它也包括各种各样的推广比如PerlBuzz,blogs.perl.org以及Ironman Blogathon,还包括世界各地数不清的YAPC和MiniYAPC。这是Perl Mongers的再次振兴,这是新的Task::*规则,这是新的Perl 5.12的发布规则。这也是Enlightened Perl之类正在做的。</p> <p>我们现在正在未来的悬崖边上,自然不能吃老本,但是可以抽点时间来赞美一下在过去这几年里我们所做出的成绩。我们成功避免了成为“过时语言”的危险,并且再次成为主流。是时候该做更多的创新了,是时候去创造一些让Ruby,Python和其他语言想抄的工具和应用了。这就是Perl的正则表达式成为事实标准的原因,也是这些语言极力想建立自己的“CPAN”的原因。让我们做更多的创新让它们去抄吧。</p> Perl6之十六 - closure traits http://sunnavy.net/entry/e2be54ec4c1811df9edac3baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/e2be54ec4c1811df9edac3baa9a5093e Thu, 07 Dec 2006 16:54:03 GMT <p>``trait"是一个比较新的词汇(起码对于我来说),Perl6里有好几个地方都用到这个词。中文直译过来是“特性”,我觉得也差不多是这个意思。通常在说明一个东西某方面的性质时便会用到这个词。在OO中,我们还将看到这个词。</p> <p>之前我们看到的CATCH block就是它所在的block的一个trait。此外还有好些,比如BEGIN, INIT,LEAVE,NEXT,END等等都是block traits</p> <p>之前说的Perl6中模拟Perl5 continue {}便可以使用NEXT block来完成</p> Perl6之十五 - exception http://sunnavy.net/entry/c2f8a9e64c1811dfbd9bb8baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/c2f8a9e64c1811dfbd9bb8baa9a5093e Wed, 06 Dec 2006 16:08:05 GMT <p>先说一下eval。Perl5中的eval接受两种参数,一个是EXPR,一个是BLOCK。Perl6中eval只接受前一种形式(EXPR),后一种形式将有个新名字try。</p> <p>Perl6的exception和别的语言不太一样。对于一个block,如果你需要进行异常处理,那么就在这个block里面放上一个CATCH block(其实它也是个closure trait)。</p> <p>异常处理其实是个switch语句,只不过这里的主题是那个异常对象(Exception object)$!(还记得么?之前说过switch是个主题化的方法)</p> Perl6之十四 - switch http://sunnavy.net/entry/91391de64c1811dfa6b2b3baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/91391de64c1811dfa6b2b3baa9a5093e Tue, 05 Dec 2006 16:20:29 GMT <p>Perl6将有``switch"语句,但keyword可不是switch和case,而是given和when。因为``switch"其实一种主题化(topicalizing,语言学里的概念)的方法。</p> <p>e.g.</p> <pre class="prettyprint"> given EXPR { when EXPR { ... } when EXPR { ... } default { ... } } </pre> <p>when后面跟的EXPR是smart match(更强大的模式匹配)。given block里并不要求全是when。</p> <p>跟C语言的switch还有一个很大的不同:在满足一个条件,执行完对应的语句后就直接退出given block,而不是向下继续执行。(还记得在C语言的switch里我们要写多少break么?呵呵)。</p> Perl6之十三 - loop http://sunnavy.net/entry/5bad31ee4c1811df8dd8aabaa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/5bad31ee4c1811df8dd8aabaa9a5093e Mon, 04 Dec 2006 17:06:58 GMT <ol> <li value="1">continue block没有了。</li> </ol> <p>新方法:在loop block里放一个NEXT block(其实是个closure trait,以后会讲,;-)</p> <ol> <li value="2">do while没有了。</li> </ol> <p>改用repeat while。跟do while不同的是,repeat while可是真正的loop block,因此可以使用next, last,redo等等。</p> <p>e.g.</p> <pre class="prettyprint"> repeat { say 'bla,bla'; } while $foo > 0; </pre> <p>还可以这样:</p> <pre class="prettyprint"> repeat while $foo > 0 { say 'bla,bla'; } </pre> <p>do {} 这种形式将被当做do-once loop,和Perl5中的bare block等价。</p> <ol> <li value="3">for分叉了。</li> </ol> <p>类似于C中的for的用法被loop取代,for只有剩下的那一种意思(即和foreach等价),于是,foreach就被舍弃了(我不用foreach已经很多年了,嘿嘿)。</p> Perl6之十二 - if http://sunnavy.net/entry/3dca6eee4c1811dfbd13a5baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/3dca6eee4c1811dfbd13a5baa9a5093e Sun, 03 Dec 2006 18:02:16 GMT <p>if和Perl5里的基本没什么区别,除了在Perl6中条件部分的括号可以省略了。其实不止if,绝大部分控制语句中相应的“条件部分”的括号都可以省略了。</p> <p>e.g.</p> <pre class="prettyprint"> if $foo eq 'Bilbo' { say 'Bilbo loves smoke-rings'; } </pre> Perl6之十一 - block http://sunnavy.net/entry/19ea83b04c1811dfbc1ba1baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/19ea83b04c1811dfbc1ba1baa9a5093e Sat, 02 Dec 2006 16:05:56 GMT <p>我们最常看到的block就是被{}包起来的那部分,一个文件也是一个block。block都是闭包(closure)。</p> <p>在block里声明的lexical变量(比如my声明的)不会泄漏到block外面去(除非你做一些手脚)。</p> <p>Perl5里的local函数被改名作temp,而且行为上还有些细微差别:temp声明时不会undef变量。</p> <p>e.g.</p> <pre class="prettyprint"> $_ = 3; { temp $_; # $_ still is 3 } </pre> <p>Perl6里增加了一个let函数,和temp基本一样,区别在于只有在block非正常退出后变量才会恢复之前的值(比如抛出异常,Scalar环境中返回undef),否则将会保持这个新值。</p> Perl6之十 - feed operator http://sunnavy.net/entry/e8f62d404c1711df9a7896baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/e8f62d404c1711df9a7896baa9a5093e Fri, 01 Dec 2006 16:28:13 GMT <p>feed operator就像UNIX里的管道(pipe),只不过这个管道里面流的是列表。符号是==>或==</p> <p>e.g.</p> <pre class="prettyprint"> @result = map { floor( $^x / 2 ) }, # It's Perl6 where comma is needed. grep {/^ \d+ $/}, # so is this @data; </pre> <p>也可以这样写:</p> <pre class="prettyprint"> @result == map { floor( $^x / 2 ) } == grep {/^ \d+ $/} == @data; </pre> <p>或者这样:</p> <pre class="prettyprint"> @data ==> grep {/^ \d+ $/} ==> map { floor( $^x / 2 ) } ==> @result; </pre> <p>Schwartzian Transform应该能用的上这个,;-)</p> Perl6之九 - cross operator(obsolete) http://sunnavy.net/entry/c7d8343c4c1711df98de91baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/c7d8343c4c1711df98de91baa9a5093e Thu, 30 Nov 2006 15:49:10 GMT <p>cross operator也是metaoperator的一种。符号是XX参数为列表,对它们进行permutation操作,类似于集合中的笛卡尔积。</p> <p>e.g.</p> <pre class="prettyprint"> 1,2 X+X 3,4 # (1+3, 1+4, 2+3, 2+4), i.e. 4,5,5,6 <a defang_b> X~X <!--defang_c defang_d--> # 'ac', 'ad', 'bc', 'bd' <!-- close inline tag into block --></a></pre><!-- reopen inline tag after block --><a defang_b> <!-- close unclosed tag --></a> Perl6之八 - reduction operator http://sunnavy.net/entry/9f31e50a4c1711df98ed89baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/9f31e50a4c1711df98ed89baa9a5093e Wed, 29 Nov 2006 16:46:17 GMT <p>reduction operator也是metaoperator。符号是[],作用有点像FP中的fold函数。</p> <p>e.g.</p> <pre class="prettyprint"> [+] 1,2,3 # 1+2+3, so we get 6 [-] 1,2,3 # 1-2-3, get -4 [~] 'foo','bar' # 'foo' ~ 'bar', get 'foobar' [] 1,2,3 # 1 2 3, get True; </pre> Perl6之七 - hyper operator http://sunnavy.net/entry/81db1c7e4c1711df8b0f84baa9a5093e "sunnavy" <sunnavy@gmail.com> article, article, Perl6, Programming http://sunnavy.net/entry/81db1c7e4c1711df8b0f84baa9a5093e Tue, 28 Nov 2006 16:44:17 GMT <p>首先解释一下metaoperator。就我的理解,它就是可以和别的运算符一起协作来干些事情的运算符。</p> <p>今天要说的Hyper运算符就属于metaoperator</p> <p>符号是>>或者,它对列表中的每一个元素在标量环境中操作,返回新生成的列表。e.g.</p> <p>- (1,2,3) # (-1,-2,-3)</p> <p>(1,2,3,4,5) >>+ (1,2,3,4,5) # (2,4,6,8,10)</p>