Sunday, August 30, 2009

Discussion about anonymous function in Perl and Ruby

Just for fun, tonight I write a methods invoking two anonymous functions to iterate a Range with specified condition in Ruby. It looks like
class Range
def each_satisfy()
end
end
And I want to invoke it as

(1..10).each_satisfy(condition_lambda, callback_lambda)

(1..10).each_satsify { |n| ...condition statment... } do |n|
...callback statement...
end
When I try to define its prototype as
def each_satisfy(&condition, &callback)
end
Ruby Interpreter broke with syntax error. So that, finally I write it as

def each_satisfy(condition, &callback)
self.each do |n|
yield n if condition.call n
end
end
and it should be used as

(1..10).each_satsify lambda { |n| ...condition statment... } do |n|
...callback statement...
end

It reminded me that Perl also has the same problem. Even when we declare

sub each_satisfy(&&) {

}

Still only first sub keyword could be omitted. That means we should invoke it as

each_satisfy { ... condition ... } sub {
my ($iter) = @_;
... callback ...
}

The only difference is Perl omitted the first (sub) and Ruby omitted the last (lambda).

I do want to figure out WHY they could not be designed to accept arbitrary quantity of anonymous functions.

 

Sunday, August 23, 2009

LWP, cURL, OpenSSL and Posterous

The fun of programming is there is always subtle mechanism in implementation. It costs time to discover but sometime is deserving. for example.

$ curl --basic -u <user> http://example.com/api/

$ echo -n "user:pass" | openssl base64 -e

and



to



 

Monday, August 17, 2009

Posterous API in Perl

Posterous.com is a new (micro)-blog mesh-up system.

You can use it mesh up almost all your (micro)-blog. Post once, Publish Everywhere. And there is still many funny features.

It looks pretty good, so I translated its API to Perl. You can get it from CPAN. Still work in progress, but would be productive soon.

 

Saturday, August 15, 2009

Can't locate Git.pm in @INC

Well, the installation of Github::Import worked well on Linux boxs but always reported "can't locate Git.pm in @INC" on Macintosh boxs. It is just a little trick and the answer is here.

Git.pm comes with git instead of being a part of CPAN. Building a copy of git-core or copying from other box solve it quickly.