December 2010 Archives

密碼產生器

| No Comments | No TrackBacks

shell function,塞到 .bashrc.zshrc 後使用。用了 pbcopy 自動把內容剪到剪貼簿(Mac 限定)。系統中無此指令時將該例移除即可。


pw () {
    p=`perl -e 'print map { chr rand(94) + 33 } 1..16';` 
    echo -n $p | pbcopy
    echo $p
}

這段程式產生的是像 I:tx)C<!:'xn((AY 這種強密碼,有用到大小寫字母、數字、符號。如果不想要這麼強,可以調整 94,33 這一組數字。例如調為 10,48,就會產生出只有阿拉伯數字的弱密碼。如果長度不用到 16 個字符,就將 1..16 的部份改成 1..12 或 1..8 之類的。

如果不需要自動前剪到剪貼簿的功能,其實可以改成 alias(注意多了一個 -l,才能適當地輸出換行):

alias pw="perl -le 'print map { chr rand(94) + 33 } 1..16';"

或是做成獨立的程式執行檔。

或是把這段程式碼背下來每次重打。

新年快樂。

文章擴寫

| No Comments | No TrackBacks

這個方法是把比較短的動詞,變成比較長,但同義的動詞或字句。像是把 <動詞> 代為成「 做一個 <動詞> 的動作」,一次就可擴寫六個字:

例如:

讓我來幫你整理

變成

讓我來幫你做一個整理的動作

而「做一個整理的動作」本身這個字句自然也是動詞的作用,那也可以再遞迴地被套用同樣的擴寫規則:

  • 讓我來幫你做一個做一個整理的動作的動作
  • 讓我來幫你做一個做一個做一個整理的動作的動作的動作
  • 讓我來幫你做一個做一個做一個做一個整理的動作的動作的動作的動作
  • 讓我來幫你做一個做一個做一個做一個做一個整理的動作的動作的動作的動作的動作
  • 讓我來幫你做一個做一個做一個做一個做一個做一個整理的動作的動作的動作的動作的動作的動作

每套用一次就會多六個字出來。

實際上使用後,就會被 zonble 評批

「讓我來幫你做一個整理的動作」到底是哪門子的中文...。

About 1.5 years ago I figured how to emulate implicit method invocation at runtime, but I never released it as a cpan module. And here it comes: self::implicit.

It's a part of self distribution, the context variable $self is automatically figured out based on the type first argument. For example, consider the following code:

sub single { $self->{number} }
sub double { $self->single * 2 }

That double routine can be simplified to be:

sub double { single() * 2 }

If we declare sub prototype to be an empty list, they can be invoked without the empty argument list:

sub single() { $self->{number} }
sub double { single * 2 }

self::implicit should not be used together with self, for you will end up with double-injected code that mess up the value of @_. It also declares $self and @args variable for you to use like self, so there really is no reason to say use self once use self::implicit is there.

Even though I put it together and released it, I'm not sure how it should evolve at the moment. It is just that this language feature is built in Ruby, and I think Perl programmers should someday be benefit from not having to say $self all the time. An OO language can force you to say self or this too much, while a functional language can force you to carry context information in its long argument lists. By properly digging $self variable from the caller scope, it makes programming much easier because the expressions that must be said is less.

A shared perlbrew root for multiple users

| No Comments | No TrackBacks

Perhaps the biggest advantage of using perlbrew 0.15, is the possibility to share one perlbrew root among multiple users, assuming everyone uses bash (or zsh, which is compatible to bash.)

Let's assume the following scenario:

  • The global perlbrew locates at /opt/perlbrew
  • an user named perlbrew has write permission, and can install new perls and modules
  • all other users has read-only access

First of all, the perlbrew user should set PERLBREW_ROOT before installing perlbrew:

export PERLBREW_ROOT=/opt/perlbrew
curl -L http://xrl.us/perlbrewinstall | bash
/opt/perlbrew/bin/perlbrew init

The perlbrew executable is then installed as /opt/perlbrew/bin/perlbrew. The init command is required at this moment but it has be working to be automatically invoked when necessary. After this, everyone else in the system can simply add these two lines to their ~/.bashrc to use perlbrew:

export PERLBREW_ROOT=/opt/perlbrew
source /opt/perlbrew/etc/bashrc

Each user now has a personal init file at ~/.perlbrew/init, which is auto-generated to store the result of perlbrew switch. Not using symlink anymore so there is no permission problem in the way. As long as users have read and exec permissions to some PERLBREW_ROOT, they can invoke perlbrew exec, perlbrew use and perlbrew switch just fine. /opt/perlbrew can simply be managed by the root user, no big deal. If you do not mind, you can share your $HOME/perl5/perlbrew with others too.

This seems to be a pretty handy outcome of ditching the symlink approach. It might not be needed by many people. However when someone needs it, I hope it will be simple enough for them to do what they want.

DuckDuckGo 介紹

| No Comments | No TrackBacks

Duckduckgo 是筆者最近主要在用的搜尋引擎。有以下幾個令人喜愛的特點:

  • 鍵盤介面 - j, k, Enter, Cmd-Enter
  • 特殊問答 - ip, pw, yes or no
  • 他站搜尋 - !cpan perlbrew, !ruby Number, !yt Emacs
  • 安全性 - 可全程使用 https
  • 匿名性 - 不記錄歷使用者的搜尋記錄

有時全文搜尋結果感覺起來並不如 Google 那麼理想,也不能調整時間做即時搜 尋等等。但由於可以用鍵盤很快地前後瀏覽(j 向上、k 向下)五十、一百筆的結果, 把想看的先開在背景的分頁(Cmd-Enter),或是開在目前分頁(Enter),讓它成了 十分有力而快速的援尋工具。

如果所搜尋的字眼,是在幾個有名的知識庫(如 WikipediaCrunchBaseWolfram Alpha)中的已知項目,那麼 DuckDuckGo 會最優先呈現出來,然後才是全文搜尋的結果。最特別的,它有特別標註「Official site」(官方網站)的功能。如果一個關鍵字對直接對應的官網,會直接出現在第一筆。DuckDuckGo Goodies 頁面說明了許多其他可用的結殊關鍵字。

DuckDuckGo 的創辨人(及程式設計師)是 Gabriel Weinberg,也是個 CPAN Author: yegg。在他個人的網站上,細詳地說明了 DuckDuckGo 伺服器的架構,甚至也回應了許多其他人的提問。大致上是 Perl / FastCGI / nginx / PostgresQL 這樣的組合。(知道它不是「又來一個 Ruby On Rails」網站後總感覺特別愉悅。)

過去討論搜尋服務多半是在討論搜尋結果的次序、排名、分數、或廣告等等,但 DuckDuckGo 在介面上方便的設計不禁讓人後退、再次用另一種不同的觀點重新去思考「瀏覽搜尋結果」這個問題。bing 也曾經在 Apple WWDC 上展示了使用 HTML5 來改善使用性的新設計,雖然目前還沒看到那樣的設計真正上線,不過筆者是非常樂見。Google 最近也常有各種改版。DuckDuckGo 的設計比較更像是「Hacker Friendly」的感覺,只要稍微學一下那些鍵盤操作方式,就能用非常有效率的方式來快速操作,而且,也並不會很難學。

安全與匿名性則是那種「失去了才會覺得重要」的東西。相對來說 Google 搜尋一直以來都會記著個人的搜尋記錄,好像有因此能得到更個人化、更「令人滿意」的搜尋結果的排序,但總是會令人掛心:你記錄我那麼多資料,倒底還會拿來做什麼?另一方面這個更「個人化」的搜尋結果是不是真的很好,有時也很難討論。畢竟,在搜尋結果的好壞時,其實有比想像中更多的主觀因素在其中。PageRank 其實還算是相當單純的演算了。

目前為止 DuckDuckGo 都是免費也無廣告的服務,十分的好心。不是很確定它們在這個服務上有沒有任何獲利,但希望這個好服務能持續下去。

Notable changes in perlbrew 0.15

| No Comments | No TrackBacks

I want to post a quick gist of recent perlbrew upgrade. New commands:

  • use
  • exec
  • install-cpanm

perlbrew is being gradually modified for better bash integration since 0.11. Notably, you can say perlbrew use perl-5.13.7 to switch to 5.13.7 only in current environment. This is possible by modifying $PATH directly, therefore perlbrew itself has become a bash function. You still need the perlbrew executable too, and it works the best with bash. Haven't figured out how to make it works for tcsh users just yet. It is still recommended for tcsh users to stay at 0.10 at this moment.

exec command is powerful. It executes the given command for each installed perls. This essentially installs Acme::EyeDrops 18 times for me (for I have that many versions of perls installed in my $HOME)

perlbrew exec cpanm Acme::EyeDrops

Try something like perlbrew exec perl -e 'print "$]\n"' and you'll see. This is a function that iterate over all installed perls and runs perlbrew use perl-xxx; sh -c '...' one by one. It changes $PATH etc automatically. Testers can find it very useful to run perlbrew exec prove -l t. The output format is not solid at all and is subject to be changed. This command will also be improved so it iterates on only specified perls. Definitely stay tuned for future updates.

install-cpanm downloads the standalone cpanm executable off from github and put it under $PERLBREW_ROOT/bin, which should be in your $PATH. This is just a handy command because we all love cpanm.

However it is not recommended to upgrade to perlbrew 0.15 quiet yet if you actually use perlbrew in you production environment. The bootstrapping / installing / initializing process is changing and not quite there yet. The init may be auto-invoked after install in the future. If you absolutely want to try perlbrew 0.15, please use the standalone executable and run install and init as if it's a brand new installation. You will not need the current symlink under $PERLBREW_ROOT/perls anymore, and it should be removed.

If you want to participate the development process of perlbrew, definitely check the github project page. File an issue for either bug reports or feature requests, and participate the issue discussion. Or, event better, fork the project and send me a pull request once you're done. That will be, absolutely no doubt, appreciated.

記得補充水份

| No Comments | No TrackBacks
0 * * * * (/usr/local/bin/growlnotify -wsm '喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水 喝水')

補記:用這種無腦的方式提醒自己喝水還真是出奇地有效。