Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Wednesday, June 11, 2008

opmsg.com 更新

opmsg 是個使用 OpenID 的網站應用程式。它是可以替代 Email 的非即時訊息傳遞系統。同時有類似「公共留言版」的功能。

為什麼說它是「替代 Email」的方案呢?如果用現有的系統比喻的話,它最像是 MSN 的離線訊息。你不知道對方現在有沒有在電腦旁邊,但你還是可以傳遞訊息 給對方,並且可以確定對方上線之後一定會收到這些訊息。

但 opmsg 與 MSN 離線訊息最大的不同是,在 MSN 上的朋友的上線狀態都是即時 的,只要你連上 MSN,不過你願不願意,MSN 都會通知大家你上線了。而 opmsg 上並不會顯示大家的上線狀態,只負責把訊息儲存好,讓大家可以閱讀。以這點而言, 則正如 Email 一般。

那 opmsg 比 Email 好在什麼地方呢?

Email 是個分散式的系統,不太能控制其他人能不能送信給你,任何人都可以很 自由地送信給任何人。雖然很方便,但也造成了今日難以解決的 SPAM 問題。雖 然有很多自動偵測信件內容是否為 SPAM 的程式,但準確度永遠不會到達 100%。 難免有許多不是 SPAM 的信件,被分類到垃圾信件夾。

而在 opmsg 上,由於訊息傳遞與儲存都是經過中央的伺服器,管理 SPAM 的難度, 基本上可以簡化成管理「好友列表」或是「黑名單」。凡是在黑名單上的使用者, 都不能傳訊息給你。此黑名單也可以公開,更有效地杜絕垃圾信件。

不過,這以的機制為何 Email 辨不到呢?這其實也是 Email 系統長久以來的一 個大問題。那是因為,Email 訊息中的「寄件人」是可以假造的。有心散佈廣告 信件的人士,可以自行架設郵件伺服器,以你好友的 Email 地址做為寄件人發信 件給你。這樣一來就算你的 Email 閱讀程式有「黑名單」功能,也沒有用,因為 你不會把好友的 Email 地址加入黑名單之中。

請大家有空時務必試試看 opmsg,並給點意見。目前需要 使用OpenID 登入,有 Flickr 帳號的話,可以直接使用他們提供的 OpenID 服務, 打入自已的 Flickr 相簿網址便行(http://flickr.com/photos/myname)。 Yahoo 與 Google 也都有提供。關於 OpenID 的一些其他想法,也請參考 淺談 OpenID 這篇文章。

Sunday, June 08, 2008

Perl 裡的 OpenID2 實做

最近繼續用 Jifty 亂寫東西,配合 OpenID,但發現目前在 CPAN 上的 Net::OpenID::Consumer 無法配合 Yahoo 的 OpenID。研究之後發現是因為 Yahoo 只提供 OpenID 2,而目前釋出的 Net::OpenID::Consumer 尚未支援 OpenID 2。

有必要使用的人,建議直接安裝 sixapart svn裡面的版本:

http://code.sixapart.com/svn/openid/branches/openid2/perl/Net-OpenID-Consumer/

自已測試之後,可用 Yahoo 與 Google 的 OpenID。

Saturday, May 17, 2008

YAPC::Asia::2008 結束了

YAPC::Asia::2008 結束了,又結束了一次世界最大的 YAPC,今年到場的人 數竟然有 529 人,請給 Shibuya.pm 鼓鼓掌。啪啪啪啪啪啪啪。兩天下來 我也重新複習了這句話的日文:拍手しでください。

我這兩天內給了兩場演講,投影片已經先張貼出來了,各 20 分鐘,而接下 來的兩天都是黑客鬆活動,地點在「Hotel Dan」。這並不是一間旅館,而 是小飼弾先生的家。他家上過電影, 是 2006 年的這部片:Babel。 是在月島的大樓上,第32層,可以俯瞰整個東京市。

我打算向 nothinmuch 請益,改進 一下self.pm,現在的寫法是在 self 函式裡利用 @DB::args 取得上一層的 $_[0] 的值,但這種做法的效 率很差,Shibuya.pm 的 Yappo 曾經比較過數種方 法,我跟 audrey 的做法類似,效率是約是直接使用 $_[0] 變數的十分之一左右。audrey 成 功地利用 tie,可使用 $self, @args, %args 等「變數」,但由於 tie 有 一些額外的負擔,所以還更慢一些。

那麼,如何改進呢?大致上的做法是,利用新的工具 Perl6::Declare,在 compile 時期直接在原碼的 op tree 上注入 "my $self = $_[0]" 這列敘 述(也是以 op tree 的形式),或是,直接樹裡面出現的 $self 改成 $_[0]。這樣一來,便等於有類似 Source filter 的效果,效率卻會更好一 點,因為注入的程式碼已經是解析好的形式。

Perl6::Declare 所提供的能力,是能在 compile 時期便能執行一段自訂的 程式碼。如果我記得沒錯的話,今年 Larry Wall 在開場的 keynote 所說 的一句話:"Parsing is is just dispatching at compile time"。也就是說,如果 能在 compile time 改變 dispatcher 而能執行一段自定程式碼,便能提供 程式語言最大的能力,使寫作者能夠改變其使用的語言本身。

Perl 6 計畫進行了這許久,除了提供大家很大的期待之外,在這兩年實做 的過程之中,也讓 Perl 人有許多挑戰計算機科學界限的地方。領悟到在 compile time 要能執行程式碼的這一著,絕對不簡單。Michael Schwarn 今天在結尾的 Keynote 說,Perl 社群是 Folk Programming 開始的。的確, 我們不像 Haskell 社群,有許多博士與博士論文去完備地推導程式語言的 各種定性。但現在 Perl 社群的核心正確實地在挑戰其他語言未曾到達的領 域。

Perl 正活著很痛快呢

Friday, May 16, 2008

JavaScript::Writer fun

The talk for my "JavaScript::Writer fun" topic. The slide is an XUL, you need Firefox browser to see it. So go get firefox.

Slide: JavaScript::Writer fun

Test-Continuous -- Tool that makes module developing happier

My talk for YAPC::Asia::2008 Day1. Too bad that it's way too short for the 20 minute slot :/ I should have make about 30 more slides, but really there isn't much things to talk about in there yet. *shrug*

Sunday, April 13, 2008

OSDC.tw 圓滿結束

以下是我在這 2008 年 OSDC.tw 裡演講所用的投影片。首先是「網頁程式還可以怎麼設計」

然後是 lightning talk 的部份。我總覺得我好像比較適合做 lightning talk。

Friday, April 11, 2008

This thing I always forgot...

Just a memo. In my Jifty App::Model::User code.. I almost always need this:

sub _brief_description { 'name' }

sub current_user_can { self->current_user->id == self->id }

Thursday, April 03, 2008

Auto-complete ssh hostname in zsh

This zshrc snippet automatically search for ~/.ssh/known_hosts for hostnames for ssh, scp, and ping command. It requires perl.

compctl -f -k "(
        `cat .ssh/known_hosts | perl -lnae '@C=split(",", $F[0]); print for @C'`
)" ssh scp ping

Saturday, March 29, 2008

HappyDesigner Meetup #3

我在這第三次 HappyDesigner 聚會的講題是 "SPA, JavaScript::Writer, pQuery",投影片如下,歡迎參考:

下次會在 OSDC.tw 的場子上演講「網頁程式還可以怎麼設計」,OSDC 的日期是 4 月 12、13 日。內容大概會跟這次有一小部份相關吧。會涵蓋過去幾次做網頁程式設計的一些經驗與心得。

Sunday, February 10, 2008

Decide if a sub calling from current package

When you're writing a Perl module, this subroutine can test if your sub is called from current package:

sub __in_current_package__ {
    my $level = 0;
    my @c = caller(++$level);
    while ( @c > 0 ) {
        return 1 if defined $c[0] && $c[0] eq __PACKAGE__;
        @c = caller(++$level);
    }
    return 0;
}

How you use it:

sub do_something {
    if (__in_current_package__) {
        return &do_this;
    }
    return &do_that;
}

Saturday, February 09, 2008

link to somewhere, the web forms

Rails has this "link_to" methods with pretty good semantic:

link_to "Gugod's website", :url => "http://gugod.org/"

Pretty much just like speaking English. In Jifty, it has several different varints. One is "hyperlink" if you use Template::Declare:

hyperlink(
    label => "Gugod's website",
    url   => "http://gugod.org"
);

In plain HTML, that's

<a href="http://gugod.org/">Gugod's website</a>

In Socialtext wikitext, that's:


   "Gugod's website"<http://gugod.org>

In Markdown, that's:


   [Gugod's website](http://gugod.org)

Each one make sense here. The frst two are designed to be written inside application's templates, the last two are designed to be written as application content. HTML are somewhere in the middle. It can be written as part of your templates, or part of your application content.

Maintianing templates of the application are usually programmer's job. Sometimes templates becomes a managable content such that users have to maintain them too. Like blogger's template, or bricolage's tempate.

Subjectively I vote for Socialtext wikitext syntax if I am a heavy content provider. For several reasons:

Backward compatible punctuations. The double quotes natually emphasizes the link label to mean: "Something different in here".

Direct. Human readers immediately catches the first part, which is the link label. Can ignore the rest of text between < and > since those characters are not likely helpful without a proper reader. Comparing this:

You must visit "Gugod's website"<http://gugod.org> and donate him.

with this:

You must visit <a href="http://gugod.org/">Gugod's website</a> and donate him.

The second one makes me stop breathing for 1.74 seconds in the middle so I can read the whole sentence. (I voicelessly read text in my head, it's a bad habbit that I have been trying to fix for 20 years since I was awared of this problem.)

The same semantic looks ok in Markdown:

You must visit [Gugod's website](http://gugod.org) and donate him.

It looks even better in Markdown with another type of syntax:

You must visit [Gugod's website][1] and donate him.

[1]: http://gugod.org

Which makes the whole article looks like a research paper with whole bunch of citation links afterwards. It's either good or bad.

The Template::Declare way to do this is quite said:

outs_raw("You must visit ");
hyperlink(
    label => "Gugod's website",
    url   => "http://gugod.org"
);
outs_raw(" and donate him.");

I'm almost out of oxygen and can't get any high. This snippet is too heavy for what it does. A sprintf-like way for these inline-level elements can work better:

outs_raw + sprintf(
    "You must visit %s and donate him",
    hyperlink(....)
)

But this doesn't work like what you'd expect due to the design of Template::Declare, another "inconvienent truth."

I feel it much lighter to be able to use just "..."<...> to represent a simple hyperlink.

But that raises a very interesting issue for framework designers: Why should this optimized to let developer put more literal links in view code ? I would also vote against for over-optimizing these things that you don't really need.

Or I would just delegate it to other formatters for such purposes, say, having a "wikitext" helper in Template::Declare:

div {
  wikiext qq{
    You must visit "Gugod's website"<http://gugod.org> and donate him.
  }
};

Or a "markdown" helper:

div {
  markdown qq{
    You must visit [Gugod's website](http://gugod.org) and donate him.
  }
};

This reminds me of my previous work, Template-Provider-Markdown, which allows you use Markdown to replace HTML in your TT2 templates.

If I have to generate a tangent link then it's a totally different story. That's something no need to be optimized. (Maybe)

Socialtext wikitext syntax is very close to be a valid piece of Perl6:

"Gugod's website"<http://gugod.org>

"" forms a string value, and <> means getting a thing from a hash with the hash key barely given inside. So that means: getting the value associated with "http://gugod.org/" from the Hash represented by "Gugod's website". Syntax error.

But there might be a chance to make this work for Perl6, since the form

$obj.<>

is a method-like postcircumfixes, it might be able to be overloaded for Strings. In fact, if this is true, the original dot-less form might just work too. That would make perl6 the best langauge to design templating system for both appllication templates and application content -- just overload a bunch of Perl6 operators such that our content can be a form of valid subset Perl6.

Sunday, February 03, 2008

JavaScript::Writer + Template::Declare

I spent last 24 hours figured out a nice way to play JavaScript::Writer with Template::Declare. It was super neat. Download the this zip and check out the source code. It depends on an future version of JavaScript::Writer module that had not been released to CPAN yet, so it won't work for you. But you can see the source code in lib/JT/View.pm, line 141:

  link_to(
      "Hello",
      href => "http://google.com",
      onclick => ajax_replace( "#area", "data/hello.html" )
  );
And line 109, for the definition of ajax_replace:
  sub ajax_replace {
      my ( $target, $uri ) = @_;
      return sub {
          jQuery("#loading")->fadeIn(
              sub {
                  jQuery("#area")->load($uri)->show(
                      sub {
                          jQuery("#loading")->fadeOut();
                      }
                  );
              }
          );
          js->append("return false;");
      };
  }
It returns a Perl sub, and eventually converted to a Javascript function that work as the onclick handler. One nice feature is, although the A tag and onclick handlers are put together in Perl code, they are seperated in HTML code. All generated javascript are put to the end of body. A elements get an auto-generated ID for latter reference. And it's all kept in perl so programmer worries nothing. Now it works great with CGI::Minimal, no model code in this spike. I want to somehow make a better integration of such concept into Jifty, or Catalyst. That feels like an assignment to myself in the coming OSDC.tw and YAPC::Asia

Wednesday, January 23, 2008

Some Perl

To see if @a is a subset of @b in Perl, use this:

  all(@a) == any(@b)
(all and any are exported from Quantum::Superposition) I think it's brilliant that that it's very different from
  any(@b) == all(@a)
Which will be true only if all elements in @a are the same as anyone one element in @b. Quantum::Superposition is so brilliant but should really be renamed or aliased to some shorter name. Or can just be a part of "feature" pragma.

Monday, November 05, 2007

self.pm

OO Perl programmers often find them self typing this line many times:

    my $self = shift;

Why ? Because Perl do not have a special keyword to represent current object, the first argument in the argument list are taken to be the current object. Such convention has been there for years.

As Ingy döt Net tried to use source filter to sort that out, his Spiffy module is actually one of my favorite module. But there are too many know problem with source filter, and turned out nobody really enjoy it at all.

Although I've always think, with Perl, one shouldn't be bundled with the language to limit their thought, but should just enjoy the TIMTODI style to get things done in your favorite style. That's why the re-invented wheels like Template-Declare, Object-Declare, Scalar-Defer, Web-Scraper are so elegant.

Without using source filter, one can always say $_[0] to get the current object, but that's not very readable.

So self.pm provides a slightly different self, it's not a variable, but just a bare-word. Really it's function call that gets your current $_[0], but it's pretty handy.

Turned out somebody really likes it, that's great. :)

Wednesday, October 24, 2007

OAuth

最近這兩個星期斷斷續續花了一些時間看 oauthSpec。先簡單瞄了一下作者群:

  • Mark Atwood (me@mark.atwood.name)
  • Richard M. Conlan (zeveck@google.com)
  • Blaine Cook (blaine@twitter.com)
  • Leah Culver (leah@pownce.com)
  • Kellan Elliott-McCrea (kellan@flickr.com)
  • Larry Halff (larry@ma.gnolia.com)
  • Eran Hammer-Lahav (eran@hueniverse.com)
  • Ben Laurie (benl@google.com)
  • Chris Messina (chris@citizenagency.com)
  • John Panzer (jpanzer@acm.org)
  • Sam Quigley (quigley@emerose.com)
  • David Recordon (david@sixapart.com)
  • Eran Sandler (eran@yedda.com)
  • Jonathan Sergent (sergent@google.com)
  • Todd Sieling (todd@ma.gnolia.com)
  • Brian Slesinsky (brian-oauth@slesinsky.org)
  • Andy Smith (andy@jaiku.com)
真是各路人馬都到位了。摘要如下:
  • 與 flickr API 所呈現出的概念十分相似。
  • 使用者可以因此在各處操作自已的資產,卻不需提供存取資產所需的機密資料
    • 資產:如 flickr 上的照片
    • 各處:如 hypoDot,hypoDot 可讀取使用者在 flickr 上的照片
    • 存取資產所需的機密資料:使用者的 flickr 帳號與密碼
  • 名詞(自譯)
    • 使用者(User):將個人資產放在提供商系統上管裡的人
    • 提供商(Service Provider):存放使用者資產的系統。
    • 消費商(Consumer):欲操作儲存在提供商之使用者資產的系統
  • Auth 同時表示 Authentication 與 Authorization
  • Spec 內容主要著重於:提供商與消費商之間如何進行交易,在經過使用者同意、認可(Authorize)的前提之下,讓消費商可以不必知道使用者的登入資訊 (Credentials),便能存取其資產。
  • 此三方交易的實做細節定義在 HTTP 之上。但其實不必限制於網站應用程式。
  • 與 OpenID 解決的問題不同,也不類似。(名稱上倒是有點容易造成誤解)
    • OpenID:讓大家知道你是誰
    • OAuth:讓你能在各處使用你自已的資產(Auth means both Authorize and Authentication)
  • 消費商不必知道你的帳號,它也不必知道你是誰。它只是要用你的資料而已。
  • 與其他「遠端登入」系統也不一樣(如 TypePad、Google Auth) 也不一樣。理由同前。
  • 在實做方面,倒是與 OpenID / TypePad / Google Auth 類似。提供商與消費商兩方要經過數次交易,交換一些機密。不同的只有最後拿到的資料,OAuth 的消費商不會需要知道使用者的帳號,但其他三者就是要知道使用者的帳號。
而 Perl 模組已經有了,近期的修改似乎也很積極,不過沒有文件:http://search.cpan.org/dist/Net-OAuth/ 怎麼說呢,有點像是,咖啡店的熟客,因為混熟了,自然會有些特權,熟客介紹來的新客,只要報上熟客的大名,就可以暫時享有部份特權...

Tuesday, October 02, 2007

JavaScript::Writer talk at Shibuya.pm

我在 Shibuya.pm Tech Talk #8 的場子上講了最近所寫的 JavaScript::Writer。在 ustream.tv 上有錄影(http://ustream.tv/yappo/videos/PR2xPU.f0uaa9ZCbUf56gg)。 只花了一個晚上就整理完畢,真是辛苦 Shibuya.pm 的工作人員了。這場 Tech Talk 也很有意思,天南地北的主題都有。每位講者都有各別的錄影影片。( http://ustream.tv/search/recorded/tag/shibuya.pm )

Wednesday, September 19, 2007

A simple Perlbal load balancer config for mongrel cluster

I use these two configuration for my little Rails app. 5 mongrel instances, balanced and externally served with Perlbal. It can still be refined to let Perlbal serves the public/ folder. But this is good enough for me at this point: mongrel_cluster.yml:

--- 
log_file: log/mongrel.log
port: 4000
pid_file: tmp/pids/mongrel.pid
servers: 5
load-balancer.conf for Perlbal
CREATE POOL my_servers
  POOL my_servers ADD 127.0.0.1:4000
  POOL my_servers ADD 127.0.0.1:4001
  POOL my_servers ADD 127.0.0.1:4002
  POOL my_servers ADD 127.0.0.1:4003
  POOL my_servers ADD 127.0.0.1:4004

CREATE SERVICE balancer
  SET listen          = 0.0.0.0:3000
  SET role            = reverse_proxy
  SET pool            = my_servers
  SET persist_client  = off
  SET persist_backend = off
  SET verify_backend  = off

ENABLE balancer

Sunday, August 05, 2007

jsmin

My ~/bin/jsmin:

#!/usr/bin/env perl
use JavaScript::Minifier qw(minify);
undef $/;
print minify(input => <>);

JavaScript::Minifier works pretty well. This is used when I'm making opcafe website, I minimized the js lib like this:

opcafe-lib.js: jemplates.js
cat Jemplate.js JSAN.js DOM/Utils.js jemplates.js jquery.js interface.js jquery.rotate.js | jsmin > $@

Monday, June 25, 2007

looks like purl watches movies too.

Just happened in pound perl...

08:44 <+    claes> good morning
08:44 <+     purl> This coffee is shit.
08:47 <+gugod> it is shit, purl
08:47 <+     purl> ...but it is nutty lately...
smashing, babie, yeah.

Sunday, March 18, 2007

iUseThis.com 開始用 openid

iUseThis.com 是一個以 Catalyst 寫成的 Web 2.0 Application,它最近開始提供了以 OpenID 登入的能力。穿針引線的 Catalyst OpenID 外掛,則是由 miyagawa 寫的 Catalyst::Plugin::Authentication::Credential::OpenID

串連 Microsoft 各種應用的 .Net Passport 自然無法與之相比。但比起新興的 Google Auth、Flickr Auth 等等,OpenID 長久以來雖然一直讓大家興趣缺缺的感覺,不過該完成的事項都還是有完成。而現在也已經有不少支持它的網站 。如 Blog 元老的 LiveJournal、後起之秀的vox.comZoomrWikitravel、也已經有 phpBB 的外掛可用。(目前支援 OpenID 的系統,完整列表請點此詳閱之。)就算是 Yahoo,也可透過idproxy來產生與 Yahoo 使用者相對應的 OpenID URL。嗯 OpenID 的登入字串格式,要是一個 URL ,非常奇怪是也。

My Bicycle Rides