Tuesday, November 30, 2010

Tasting Perl6

At the moment that the first release of Rakudo Star has been announced over four months, binary packages or tested build in package managers have been provided in varieties of OS. If we want to taste Perl6 on Mac OS X, the best choice seems to be using Homebrew. Just type in $ brew install rakudo-star and run $ perl6. Rakudo Star in Homebrew has been updated to the newest version: 2010.10.

On Debian-like system, the best one should be using PPA of dexter on LaunchPad. The version falls behind Homebrew a little to be 2010.09. Because the Rakudo Star requires Parrot 2.8+, we also have to add PPA of Parrot to satisfy. Command $ sudo apt-add-repository ppa:dexter/rakudo-pkg ppa:dexter/parrot-pkg and run update, install. Then have fun!

Tuesday, November 23, 2010

Intro to App::CLI

My first meet with App::CLI is in the summer of 2008, trying to write JiftyX::Fixtures. Without abundant PODs coming with this module, it took me almost one day to understand this module through trial and error. Since, I write this intro to reveal more power from this module; even I have contribute some code and DOCs to the project.

Actually, App::CLI is a really powerful module having central idea similar to the dispatcher in many web application frameworks such as Rails. When we are trying to create yet another handy CLI tool for our daily working, here is a recommended architecture to use App::CLI:

-> The kernel of our new tool

-> All other module forming the mechanism
-> of our new tool, e.g. MyApp::Config
-> for loading configuration

-> The dispatcher serving the invoking commands

-> All subcommands invoked by MyApp::Command

-> All subsubcommands invoked by specific subcommand

-> subsubsubcommands *god*

After deciding what command we need to implement, we only have two things should be done. The first is making MyApp::Command be able to dispatch. What we need to do is only add use base qw(App::CLI); into it.

Now, in MyApp.pm or our script, where we want to invoke command, we can just say
MyApp::Command would automatically use @ARGV to determine what (sub)command it should invoke and find its package to *require*. For example, if we type $ myapp list user --sort age in terminal, the @ARGV would be qw(list user --sort age), and MyApp::Command would require MyApp::Command::List::User, create its instance as $cmd, assign $cmd->{sort} as 'age', finally invoke $cmd->run_command().

So, The second thing, most of we need to effort, is to implement our (sub)commands. it's a bit like writing Controllers of Rails. If the URL matches /foo/bar/:id, the Action #list() of Controller Foo::Bar would handle the request, and @id would be assign value. If users type $ myapp list nickname --name /mark/ MyApp::Command::List::Nickname would handle the command and $instance->{name} would be assign string /mark/. For this case, we can write as below.

package MyApp::Command::List;
use base qw(App::CLI::Command);
use constant subcommands => qw(User Nickname);
use constant options => (
"h|help" => "help",

sub run {
my ($self, @args) = @_;
if ($self->{help}){
# output PODs when $ myapp list --help
# do something when user type like
# $ myapp list arg1 arg2 --opt1 --opt2 opt_arg2
# and arg1 doesn't equal to User or Nickname

package MyApp::Command::List::Nickname;
use base qw(App::CLI::Command);
use constant options => (
"name=s" => "name",

sub run {
my ($self, @args) = @_;
$name = $self->{name} #=> /mark/
# query the data base via condition /mark/

It completes. Note the we can specify all possible subcommands in constant subcommands of and give all possible options in constant options to all (sub)commands and just implement &run() the (sub)command would be done. And finally, note we can cascading subcommands infinitely. That is the core power of this module starting from v0.2.


Saturday, November 13, 2010

Keep favorite info pages in $HOME

Info is a handy document system most emacs fans love. We can learn Info, Emacs Basics, Emacs Lisp, Magit and blah blah blah through the system and look up any detail every time we need without leaving Emacs, so that I install many info pages into /usr/local/info, /usr/share/info and /usr/share/local/info. However, when I need to migrate all data to new computer, keeping my info pages is really a nightmare. So I add a few of lines shown below into ~/.emacs

(append Info-default-directory-list

and put all info pages I collected before into ~/.info

Now I just need to migrate all files in my $HOME to new computer.

Wednesday, October 27, 2010

Two ZSH conventions for switchers from BASH

When starting to learn UNIX, I enjoyed with the well-designed Debian 3.1 and the bash completions coming with it. At that time the ZSH on Debian wasn't well configured for a newbie having no direction in dark.

Actually, the ZSH was set as a handy tool on recent releases of Debian-like system. For a longtime user of BASH, the default setting of backward-kill-word is confusing: it never stop with DASH, UNDERSCORE, and SLASH, etc. However, it's easy to modify the action through manipulate the $WORDCHARS variable which including all chars treated as alphabet in ZSH. For example,
exclude the dash sign from $OWRDCHARS, or you can just export the $WORDCHARS you want.

The second is ZSH can only auto-completed from first choice to the final by default. Adding bindkey '^[[Z' reverse-menu-complete to ~/.zshrc makes me not being afraid of pressing tab too fast.

[1] stackoverflow.com
[2] zsh mailing list

Saturday, October 23, 2010

Fullscreen Emacs on Mac OS X from tarball

Emacs 23.2 in trunk doesn't support fullscreen on Mac OS X.

Japanese hacker typester do a patch for this.

Following is a quick step-by-step instruction to compile emacs 23.2 supporting fullscreen with this patch.

cd /tmp

wget http://ftp.twaren.net/Unix/GNU/gnu/emacs/emacs-23.2.tar.gz

wget http://github.com/downloads/typester/emacs/feature-fullscreen.patch

tar xzvf emacs-23.2.tar.gz

cd emacs-23.2

patch -p1 < /tmp/feature-fullscreen.patch

./configure --with-ns


sudo make install

mv nextstep/Emacs.app ~/Applications

[1] Emacs Wiki
[2] sanityinc.com
[3] Citizen428

Thursday, August 5, 2010

practical python trick about static variables

It seems there is no syntax to declare static variables in Python. Fortunately, here is a useful trick to accomplish the goal.

Sunday, April 11, 2010

sudo on OpenLDAP

sudo 官方文件這篇 SOP (on Ubuntu) 算是寫得最實用的文件。



1. 準備好可以使用的 OpenLDAP 伺服器,這份文件 寫得比較詳盡。幫他加上 sudo.schema,並用 migration_sudo.pl 轉換並匯入 sudoers。

2. 裝好支援 LDAP 的 sudo。Debian-like System 是 sudo-ldap 這個套件。

3. /etc/nsswitch.conf 加上 sudoer: ldap

4. /etc/ldap/ldap.conf 或是 /etc/sudo-ldap.conf (在 Debian 是同一檔案) 加上 sudoers_base [SUDOers DN]

Saturday, March 27, 2010

Fun Weekend


在 GMap 的帶路下,很快就到了信義路上的鼎泰豐





師大路小提琴表演一 from Shelling Ford on Vimeo.


師大路小提琴表演二 from Shelling Ford on Vimeo.




Common Abbreviation in LDAP

DC: Domain Country
OU: Organization Unit
CN: Common Name
DN: Distinguish Name
RDN: relative distinguish name
LDIF: LDAP Interchange Format
DIT: Directory Information Tree

Monday, February 1, 2010

FDTD tips

When using BPML, external wall should be treated as PECs for simplicity no matter how many the maximum sigma is. The best way to do that is setting initial field value on external wall to be zero, and then don't update field value of any point on the wall.

That is to say, it could be written as

Saving a few of computing resource.


Friday, January 29, 2010

Injecting one module into another in Python

Here is a simple code snippet to do that. Saving it to an individual file and import it in other module to use it.

Using exec statement is also good, but need to pass globals() or locals() dictionary to the function executing exec statement.

Doing dirty things in Python is really not easy...:/