Moose attribute didn't allow the reference rather than a CODE reference as a default value. That prevents the objects shared a attribute when initializing. Actually this is a good design. But How should I do when needing a shared attribute between objects? I may refer this as the concept of class variable.
The state variable in CODE reference may be competent with this role.
A Counter as an example here. We define a Counter class here.
A counter has a attribute "value" and a method "inc" to increment it.
Then We define a Person class.
The Person class has a attribute "name" and a shared attribute "count". The Counter is a state variable return from a anonymous CODE reference. That's the trick.
The BUILD hook increment the counter everytime a new Person is constructed.
Then we examinate the shared variable.
A global variable in package name may still work. But this way uses 'has' syntax sugar. :p
If you use builder to replace the default option. the subroutine used by the builder is just the class variable accessor from Class name, returning value as $person->count().
Saturday, March 31, 2012
Saturday, March 3, 2012
Mysterious Timestamp
時間轉換大概是最惱人的「小」問題之一,每個語言的標準處理方式不盡相同。Perl 的 localtime() 是月份從 0 開始到 11 結束,Ruby 的 DateTime 是月份是從 1 開始的,不過 Perl 的 DateTime 月份也從 1 開始 (LOL)。背誦這些東西大概會發瘋。
不過 Epoch 沒有這個問題,任何語言上的 Epoch 應該都是一致的,有的只是時差問題,使用 Epoch 來初始時間物件大概是避開這問題的好方法之一,特別在不同語言間交換時間資料時,用 Epoch 交換後怎麼 format 都不會出問題。所以有些時間模組大概會像 Perl 的 DateTime 一樣實作 from_epoch() 這種 constructor。
就這點設計而言,Badger::Timestamp 大概是最好用的時間模組,其 constructor 支援 DateTime 模組的方式初始化外,還可以餵食 Epoch 和 ISO 格式的時間字串。一個簡單的 Epoch/ISO converter 只要八行。
什麼,你問說這八行程式有什麼用,有些跑了好幾年的 unix 系統上有著 crontab 執行著需要 timestamp 來分辨執行區段的分析器,有些要當天的零分零秒,有些要隔天的零分零秒。壞掉時有人會要你一定要「人工」處理,這八行就就是這樣來的。
附帶一提, Javascript 的 Date.UTC 和 Perl 的 localtime() 一樣,都是月份從 0 開始。
和 Badger::Timestamp 很像的 moment.js這時候就很好用了,其 constructor 用陣列初始化時雖然還是沒有改用 1 作為月份起始著實也讓我驚訝了一下。好險,這 constructor 支援用 Epoch 初始化(不過承襲 javascript 的慣例,是 millisecond),還可以用 Date.parse() 支援的字串初始化。這兩招可以避開月份的問題。
不過 Epoch 沒有這個問題,任何語言上的 Epoch 應該都是一致的,有的只是時差問題,使用 Epoch 來初始時間物件大概是避開這問題的好方法之一,特別在不同語言間交換時間資料時,用 Epoch 交換後怎麼 format 都不會出問題。所以有些時間模組大概會像 Perl 的 DateTime 一樣實作 from_epoch() 這種 constructor。
就這點設計而言,Badger::Timestamp 大概是最好用的時間模組,其 constructor 支援 DateTime 模組的方式初始化外,還可以餵食 Epoch 和 ISO 格式的時間字串。一個簡單的 Epoch/ISO converter 只要八行。
什麼,你問說這八行程式有什麼用,有些跑了好幾年的 unix 系統上有著 crontab 執行著需要 timestamp 來分辨執行區段的分析器,有些要當天的零分零秒,有些要隔天的零分零秒。壞掉時有人會要你一定要「人工」處理,這八行就就是這樣來的。
附帶一提, Javascript 的 Date.UTC 和 Perl 的 localtime() 一樣,都是月份從 0 開始。
為什麼Javascript的月份是從0開始的 (-_-) w3schools.com/jsref/jsref_ut…
— Alec Chen (@alec929) February 28, 2012
和 Badger::Timestamp 很像的 moment.js這時候就很好用了,其 constructor 用陣列初始化時雖然還是沒有改用 1 作為月份起始著實也讓我驚訝了一下。好險,這 constructor 支援用 Epoch 初始化(不過承襲 javascript 的慣例,是 millisecond),還可以用 Date.parse() 支援的字串初始化。這兩招可以避開月份的問題。
Labels:
javascript,
perl
Thursday, October 27, 2011
meet git-p4
For some reasons, in recent works I have to use Perforce, the revision control system used by Perl core team before 2008. Unfortunately, perforce lacks of distributed features and leads to heavy branching. In this developing, these two problems often interrupted programmers with conflicts hardly to solve. Bad workflow design also makes members forget to check out before editing randomly. A git subcommand git-p4 lets you use the great tool git locally with the smooth integration to perforce server.
The steps to configure on kernel.org are currently unavailable. The official site of Perforce saved a copy. Following normal steps to write .p4config to setup p4client and install p4 and git-p4 into $PATH,
By the way, if you use Mercurial there is also a perforce extension can enable hg to clone perforce repository directly with a few of setup.
However, I strongly recommend perforce users migrating to modern distributed revision control system. After all, all the best companies and organizations have given Git / Mercurial a big hug, such as Github, BitBucket, Apple, Google Code, Perl, Rails, Gnome, Debian, Android and Linux Kernel. The git-p4 is a good transformer.
2012-04-26 update:
The git-p4 is now part of the git distribution and the version on github is no longer updated and doesn't work.
The steps to configure on kernel.org are currently unavailable. The official site of Perforce saved a copy. Following normal steps to write .p4config to setup p4client and install p4 and git-p4 into $PATH,
$ git p4 clone //depos/path /local/path would just work. After local commit of git, $ git p4 rebase && git p4 submit works just like $ git pull --rebase && git push to remote git repository.By the way, if you use Mercurial there is also a perforce extension can enable hg to clone perforce repository directly with a few of setup.
However, I strongly recommend perforce users migrating to modern distributed revision control system. After all, all the best companies and organizations have given Git / Mercurial a big hug, such as Github, BitBucket, Apple, Google Code, Perl, Rails, Gnome, Debian, Android and Linux Kernel. The git-p4 is a good transformer.
2012-04-26 update:
The git-p4 is now part of the git distribution and the version on github is no longer updated and doesn't work.
Saturday, April 9, 2011
Arrange bashrc
A simple recursive function can have aid on arranging bashrc files in a clear structure.
Add a line to
If a bunch of bash completions were put inside
You may also have many aliases, environment variables, function definitions for different OS like this
Just one another line
Add a line to
source it in ~/.bashrc first, and then collect similar rc files into directories respectively.If a bunch of bash completions were put inside
~/.bash_completion/, like ~/.bash_completionjust one more line
|-- hg
|-- git
| |-- git
| |-- gitk
| `-- git-common
`-- svk
load $HOME/.bash_completionwould load whole completions.
You may also have many aliases, environment variables, function definitions for different OS like this
~/.bashrc.d
|-- linux
| |-- aliases
| |-- env
| `-- functions
|-- freebsd
| |-- aliases
| |-- env
| `-- functions
`-- darwin
|-- aliases
|-- env
`-- functionsJust one another line
load "$HOME/.bashrc.d/$(lower $(uname))"can load correct system-specified rc files.
Friday, March 25, 2011
MySQL 5.5.10 homebrew install issue
Coming after the mysql 5.1.52 homebrew install issue, the just released mysql 5.5.10, which solved the previous problem, has another issue.
When starting mysql daemon with
Adjust
When starting mysql daemon with
mysql.server script, it showed error message likeERROR! The server quit without updating PID file (/usr/local/var/mysql/data/*********.pid). The new homebrew instruction to install mysql makes its default $datadir to be $basedir/data but the setting of $datadir in mysql.server line 70 didn't be fixed to corresponded to the change.Adjust
$datadir in script or just use boot parameter --data-dir=/usr/local/Cellar/mysql/5.5.10/data/ makes mysql.server work.
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
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
$ 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:
After deciding what command we need to implement, we only have two things should be done. The first is making
Now, in MyApp.pm or our script, where we want to invoke command, we can just say
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
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.
Enjoy.
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:
MyApp
-> The kernel of our new tool
MyApp::*
-> All other module forming the mechanism
-> of our new tool, e.g. MyApp::Config
-> for loading configuration
MyApp::Command
-> The dispatcher serving the invoking commands
MyApp::Command::*
-> All subcommands invoked by MyApp::Command
MyApp::Command::*::*
-> All subsubcommands invoked by specific subcommand
MyApp::Command::*::*::*
-> 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->dispatch();
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.
Enjoy.
Labels:
perl,
perl module
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
and put all info pages I collected before into ~/.info
Now I just need to migrate all files in my $HOME to new computer.
/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
(custom-set-variables
'(Info-default-directory-list
(append Info-default-directory-list
'("~/.info"))))
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,
The second is ZSH can only auto-completed from first choice to the final by default. Adding
References:
[1] stackoverflow.com
[2] zsh mailing list
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,
export WORDCHARS=${WOARCHARS:s/-//} 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.References:
[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.
Reference:
[1] Emacs Wiki
[2] sanityinc.com
[3] Citizen428
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
make
sudo make install
mv nextstep/Emacs.app ~/Applications
Reference:
[1] Emacs Wiki
[2] sanityinc.com
[3] Citizen428
Subscribe to:
Posts (Atom)