April 26, 2013

mojo tt2 defaults layout patch

Fayland And Programming

You wrote code with open source like CPAN. You meet problems with them. You investigate, write tests then provide patches. Your patches are accepeted.

I really enjoy the way it moves.

Here is another patch for Mojolicious::Plugin::TtRenderer.

the issue is that $self->defaults(layout => 'wrapper'); is always dropping the [% content %] without reason. and with debug I see the content is really in $c->stash->{'mojo.content'}. so a simple patch is that just $c->stash->{content} ||= $c->stash->{'mojo.content'}->{content};

recently I’m writing another new app with bitcoin <-> litecoin and other virtual coins exchange. and I have a wrapper.html.tt for the whole site. but I do not want to use it in the email TT2 render.

if I put WRAPPER => 'layouts/wrapper.html.tt', in the TT2 options, it will also apply to the mail render. a better approach is to use defaults layout. then in email render, set the layout as empty or actually we need use partial => 1.

MainApp.pm

$self->plugin('tt_renderer');
$self->defaults(layout => 'wrapper');
$self->renderer->default_handler( 'tt' );

OtherController.pm

my $body = $c->render(
    template => 'emails/forgot_pass', format => 'mail',
    other_stash => $var,
    partial => 1
);

partial => 1 will set layout as null. so it will give us what we want.

Have fun!

April 26, 2013 04:00 PM

April 11, 2013

Mojolicious TT2 patch

Fayland And Programming

I am kindy busy with adding new features to my job site

but it’s very boring to restart the app on template updates. TT2 should be able to pick up the changes automatically. it works under Dancer or Catalyst, but not Mojolicious.

I thought I should spend some time on investigating why before writing more stuff. and after a while, I have a patch for Mojolicious::Plugin::TtRenderer.

it dues to the Provider in TtRender always return 1 instead of mtime on _template_modified.

plicease accepts it quite fast with slight changes but I’m waiting for a CPAN release. :-)

here is few changes on http://findmjob.com/ recently.

Happy hacking!

April 11, 2013 04:00 PM

April 09, 2013

Dancer to Mojolicious

Fayland And Programming

The progress went smoothly because all of them are just Perl code.

old Dancer code: FindmJob::WWW

new Mojolicious code: FindmJob::WWW, FindmJob::WWW::Root and others

Few notes

hooks

from before_template_render to before_render

Dancer forward VS Mojolicious before_dispatch

we can modify the req->url->path in before_dispatch with assigning stash.

# feed.(rss|atom) and /p.2/
$self->hook( before_dispatch => sub {
    my $self = shift;

    my $p = $self->req->url->path;
    if ($p =~ s{/feed\.(rss|atom)$}{}) {
        $self->stash('is_feed' => $1);
    }
    if ($p =~ s{/p\.(\d+)(/|$)}{$2}) {
        $self->stash('page' => $1);
    }
    $self->req->url->path($p);
});

supervise

Dancer:

plackup -E production -s Starman --workers=3 -l /tmp/findmjob.sock -a /findmjob.com/www/bin/app.pl

Mojolicious:

hypnotoad -f /findmjob.com/bin/www.pl

Note -f is important here, or supervise will keep restarting the script.

above plackup using sock and hypnotoad use port. so we need update the ngnix config a bit.

and it looks like hypnotoad is working better than plackup Starman

Template name conversation

there is no rule in Dancer for the Template name. but in Mojolicious, we have to do it with $name.$format.$handler

in this case we have to rename index.tt2 to index.html.tt

Conclusion

well, I’m not saying that Dancer is worse than Mojolicous or any other words. both of them are great.

but Dancer is a bit messy with Dancer and Dancer2. actually I love Moo a lot, but I don’t want to spend time on upgrading Dancer to Dancer2 (there seems some more difference than expected.)

in the other hand, Mojolicious looks amazing, sri improves it every week and I admire/trust his professional knowledge.

Have fun.

April 09, 2013 04:00 PM

March 29, 2013

Two new CPAN modules

Fayland And Programming

I got two new CPAN modules uploaded today.

WWW::SpinnerChief

I wrote this based on the Python code

it’s pretty good that you know how to read the Python.

Business::PayPoint

aka tips to write SOAP module in Perl.

the PHP SOAP lib is pretty good while the Perl one is a little suck.

so usually if I can’t write it at the first try in Perl, I would use PHP to find the sending request/response

$client = new SoapClient('https://www.example.com/Test?wsdl', array(
    'trace' => 1,
) );
$client->__call('TestAction', $params);
echo "RESPONSE:\n" . $client->__getLastResponse() . "\n";
echo "REQUEST HEADER:\n" . $client->__getLastRequestHeaders() . "\n";
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";

After I got the correct sample request, I’ll try to use Perl library to send the same XML with trace on.

use SOAP::Lite +trace => 'all';

well, you even can’t write correct one sometimes (maybe I am a little stupid).

but I have a final trick for it. use XML::Write to genereate the request XML. then use

my $som = $soap->call($method, SOAP::Data->type('xml' => $xml));

you won’t be wrong in this case. :)

Have fun.

March 29, 2013 04:00 PM

March 17, 2013

Net::Telnet with bitflu

Fayland And Programming

Even Net::Telnet is quite old, it’s still very powerful and simple to use.

use strict;
use warnings;
use Net::Telnet ();
use Data::Dumper;

my $host = '0';
my $port = 4001;

my $telnet = Net::Telnet->new();
unless ($telnet->open( Host => $host, Port => $port, Timeout => 30 )) {
    die "Can't connect to $host:$port\n";
}

$telnet->waitfor('/bitflu> /');

my @messages = map { chomp; $_ } $telnet->cmd(String => 'ls');
pop @messages if $messages[-1] eq 'bitflu';
print Dumper(\@messages);

$telnet->close();

there are some tips:

remove ANSI color

foreach my $msg (@messages) {
    # if you do not remove this, your regex with /^\[/ may break
    $msg =~ s/\e\[[\d;]*[a-zA-Z]//g;
}

Change Window Size

if you use term to do bitflu> ls, you’ll see full torrent name. but with the code above, you can only see few chars. here is the note to change the Window Size:

my $telnet = Net::Telnet->new();

$telnet->option_callback( sub { return; } );
$telnet->option_accept(Do => 31);

unless ($telnet->open( Host => $host, Port => $port, Timeout => 60 )) {
    die "Can't connect to $host:$port\n";
}
$telnet->waitfor('/bitflu> /');

## copied from http://blog.webdir.bg/perl-apache-realtime-output-from-script/
## Many Thanks!
$telnet->telnetmode(0);
$telnet->put(pack("C9",
                  255,                  # TELNET_IAC
                  250,                  # TELNET_SB
                  31, 0, 200, 0, 0,     # TELOPT_NAWS
                  255,                  # TELNET_IAC
                  240));                # TELNET_SE
$telnet->telnetmode(1);

March 17, 2013 04:00 PM

March 10, 2013

Learn Python Note 1

Fayland And Programming

I’m starting learning Python a bit.

argparse

if you have args setup in different modules, like you want setup --log on logging, setup --tor --skip-tor on requesting, the combination of add_help=False and parse_known_args is pretty cool.

import argparse
parent_argparse = argparse.ArgumentParser(add_help=False)
parent_argparse.add_argument('--log', action='store', default='ERROR', dest='log', help='logging level: DEBUG, INFO, WARNING, ERROR, CRITICAL')
_args, _args_unknown = parent_argparse.parse_known_args()

afterwards, in real place, do

parser = argparse.ArgumentParser(description='Some real CLI', parents=[parent_argparse])

init.py and as

actually it’s a very cool feature. you can put some common code for all sub-modules in __init__.py like config code (to avoid write another file)

import ConfigParser
try:
    cfg = ConfigParser.ConfigParser()
    cfg.read(['/somwhere/conf/project.ini', '/somwhere/conf/project_local.ini'])
except ConfigParser.Error, e:
    logger.critical('Cannot read configuration file, reason [%s]' % e)

and as is pretty amazing.

from modulename import cfg as project_config

BeautifulSoup is another HTML::TreeBuilder

BeautifulSoup works like Perl HTML::TreeBuilder.

from bs4 import BeautifulSoup
soup = BeautifulSoup(text)

for tag in soup.find_all('span', attrs={'id': re.compile('^ctl00_contentPageContent_lbl(.*?)Value$')}):
    id = tag['id']
    id = id.replace('ctl00_contentPageContent_lbl', '').replace('Value', '')
    data[id] = tag.get_text()

something I do like in Python

def with args and default values. (Perl sub was developed too many years ago.)

you don’t need type so many {} or ().

something I do not like in Python

string interpolation is quite inconvenience. there are %, format and Template. but I really miss the $ with Perl.

I miss Perl regexp a lot. the re.compile, re.sub, re.I is too much typing.

Disclaim

I’m still a newbie. so parden me if anything is wrong. I’ll write more when I learn more. and I admit it’s pretty fun to learn Python.

March 10, 2013 04:00 PM

March 06, 2013

add alert note for old posts in Jekyll

Fayland And Programming

add alert note for old posts

For Jekyll Bootstrap, it’s quite simple as we can do something like below:

edit _includes/themes/twitter/post.html, add

{% assign page_year = page.date | date: "%Y" %}
{% if page_year < '2010' %}
<div class='alert alert-info'>This post may be outdated due to it was written on {{ page_year }}. The links may be broken. The code may be not working anymore. Leave comments if needed.</div>
{% endif %}

20 items in rss/atom

I have another if in the {% for post in site.posts %} for rss.xml and atom.xml

{% if forloop.index < 20 %}
...
{% endif %}

paginate

edit _config.yml

paginate: 10
paginate_path: 'page/:num'

Have fun.

March 06, 2013 04:00 PM

Blogger local file > Jekyll migrator

I have an old Blogger before I move to MT5, it contains my posts from 2006 to 2009. some are quite old, broken, useless and even not worth reading. but some are still very useful and it’s a part of my memory and life. (Sorry that comments are dropped that I can’t import it into Disqus.)

so I wrote a simple Perl script to convert the local HTML files into Jekyll here. the source code can be found in github.

it’s quite simple and maybe just fit for my need. but feel free to change it if you have same demand.

Have fun.

March 06, 2013 04:00 PM

March 05, 2013

MT > Jekyll perl migrator

Fayland And Programming

I bought a new domain fayland.me.

I picked Jekyll as the blog engine.

I wrote a simple Perl script to convert my old Movable Type 5 blogs to Jekyll Bootstrap.

Github code

the difference between this one and the Jekyll::MT is that it supports tags and it’s for Jekyll Bootstrap.

at last, using

rsync -arv --delete _site fay:/srv/www/fayland.me/

to rsync the site.

Have fun.

March 05, 2013 04:00 PM

November 21, 2012

Net-Amazon-DynamoDB

Fayland And Programming

I have been playing with Net::Amazon::DynamoDB a lot recently. this article is not to comment on that service. it's just a few notes for Perl guys who're using it.


1. Moo based instead of Moose.

well, if you're not using Moose in your system, Moose may be too much for your speed. I am maintain a new branch on that which converted Moose to Moo. changes can be found at https://github.com/fayland/Net-Amazon-DynamoDB/commit/0b3b9a493e92c85461353ef1fc1da422e4d0bb48 and I'll update when the Moose version is updated.

2. custom Cache module supports.

I removed the isa => 'Cache' in the Moo version, instead, I'm using

has cache => ( isa => sub {
     die "thaw/freeze/remove must be supported in cache" unless $_[0]->can('thaw') and $_[0]->can('freeze') and $_[0]->can('remove')
}, is => 'rw', predicate => 'has_cache' );

so that we can use custom layer like

package My::Dummy::Cache::FastMmap;

use Moo;
use Cache::FastMmap;

has 'cache' => (is => 'lazy');
sub _build_cache { Cache::FastMmap->new(share_file => '/tmp/mycache_fastmmap', unlink_on_exit => 0) }

# Cache::FastMmap do not have thaw/freeze sub
sub thaw {
    my $self = shift;
    $self->cache->get(@_);
}
sub freeze {
    my $self = shift;
    $self->cache->set(@_);
}
sub remove {
    my $self = shift;
    $self->cache->remove(@_);
}

1;

I have fixed some bugs for the cache system like not set while return is undef, delete on batch_write etc. but it is still not perfect b/c it's not supported inside batch_get_items yet. I may write patch for it later.

3. set is not array.
well, for SS or NS. it's set. it's not array. it means there is not an order inside.
if you want something like order, we can join it as string and save it. after get, split back.

Thanks.

November 21, 2012 04:00 PM

October 10, 2012

Care each other

purl in your heart

凡事不可自私自利、爱慕虚荣,要心存谦卑,看别人比自己强。 各人不要只顾自己的事,也要为别人的需要着想。 腓立比书 Phil2.3-4.CCB http://bible.us/Phil2.3.CCB

Posted via email from purl's posterous

October 10, 2012 09:31 PM

October 08, 2012

诗篇 118:14-16

purl in your heart

耶和华是我的力量,是我的诗歌;�拯救了我。义人的帐篷里传出胜利的欢呼声:“耶和华伸出右手施展了大能!耶和华高举右手,耶和华的右手施展了大能!” http://bible.us/Ps118.14.CCB

Posted via email from purl's posterous

October 08, 2012 05:51 PM

October 01, 2012

http://bible.us/Ps69.12.CCB

purl in your heart

我成了街谈巷议的话题,醉汉作歌取笑我。可是,耶和华啊,在你悦纳人的时候,我向你祷告。上帝啊,求你以你的大爱和信实拯救我。

Posted via email from purl's posterous

October 01, 2012 12:51 AM

September 27, 2012

http://bible.us/Ps60.4-5.CCB

purl in your heart

但你赐给敬畏你的人旗帜,可以挡住箭羽(或译为可以为真理飘扬)。求你应允我们的祷告,伸出右手帮助我们,使你所爱的人获救。 诗篇 Ps60.4-5.CCB http://bible.us/Ps60.4-5.CCB

Posted via email from purl's posterous

September 27, 2012 11:45 PM

Romans 3:23-24

http://bible.us/Rom3.23.CCB 因为世人都犯了罪,亏欠上帝的荣耀, 但蒙上帝的恩典,靠着基督耶稣的救赎,世人被无条件地称为义人。

Posted via email from purl's posterous

September 27, 2012 08:21 PM

?? 52:8-9 CCB

我就像上帝殿中的一棵橄榄树,枝繁叶茂,我永永远远信靠上帝的慈爱。 上帝啊,我要永远赞美你的作为。我要在你忠心的子民面前仰望你美善的名。

Posted via email from purl's posterous

September 27, 2012 03:20 AM

诗篇 55:6-8, 16, 22 CCB

啊,但愿我能像鸽子展翅飞去,得享安息。 我要飞到远方,住在旷野。我要赶快躲进避难所,避过暴雨狂风。 但我要呼求耶和华上帝,祂必拯救我。 把你的重担卸给耶和华,祂必扶持你。祂必不让义人跌倒。

Posted via email from purl's posterous

September 27, 2012 02:41 AM

September 19, 2012

BJ Rental price as a chart

purl in your heart

September 19, 2012 04:55 PM

September 13, 2012

reCaptcha and lightbox and ajaxPost

Fayland And Programming

sometimes, you want to do lightbox for Email Us or Contact Us page.

in this case, you don't want to include the recaptcha js in every page, you want to include it in the popup lightbox but here we have an issue that $(document).ready would be called while the google recaptcha js is not loaded. b/c document ready is just checking the original page instead of the lightbox. in this case, here is a common solution for every js loading (wait until that js loaded.)

<script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var reCaptcha_timer;
        reCaptcha_timer = setInterval(function(){
          if (typeof(Recaptcha) != 'undefined') {
             clearInterval(reCaptcha_timer);
             CreateReCaptcha();
          }
        }, 50);
    });
    function CreateReCaptcha() {
        Recaptcha.create("public_key_blabla", 'captcha-placeholder', {
            theme: "white",
            callback: Recaptcha.focus_response_field
        });
    }
</script>

see we have a tricky that keep check if Recaptcha is inited (which will be done when recatpcha_ajax.js is loaded).
and only after it's loaded, we clear the check, and create the catpcha.

the trick works pretty good. but here is another issue, when the lightbox is loaded and user submits the form, and if some elements are wrong or captcha is wrong, that we need show captcha once again. it will be broken. b/c Recaptcha js variable is already defined, and Recaptcha.create will not working fine b/c it has something stored for previous captcha.

in this case, another tricky is much more simpler.

<script type="text/javascript">
// so that we can reload it
if (typeof(Recaptcha) != 'undefined') Recaptcha = undefined;
</script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>

before load the js, we reset it so it works like it's the first time we are trying to create reCaptcha.

those issue only happens on ajax lightbox load reCaptcha and ajaxPost the reCaptcha form.
stupid but works.

Thanks.

September 13, 2012 04:00 PM

September 11, 2012

MacOSx homebrew postgresql issue note

Fayland And Programming

after brew install postgresql


then run "initdb /usr/local/var/postgres -E utf8", may got:

selecting default shared_buffers ... 400kB
creating configuration files ... ok
creating template1 database in /usr/local/var/postgres/base/1 ... FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  Failed system call was shmget(key=1, size=2138112, 03600).
HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space, or exceeded your kernel's SHMALL parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMALL.  To reduce the request size (currently 2138112 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
The PostgreSQL documentation contains more information about shared memory configuration.
child process exited with exit code 1
initdb: removing data directory "/usr/local/var/postgres"

fixes as:

 ~  sudo sysctl -w kern.sysv.shmall=1024000
kern.sysv.shmall: 8192 -> 1024000
 ~  initdb /usr/local/var/postgres -E utf8 

then everything will move smoothly.

just for note.

September 11, 2012 04:00 PM

August 18, 2012

Proverbs 21:23-24

purl in your heart

http://bible.us/Prov21.23.CCB 管住口舌,免遭祸患。嘲讽者狂妄自大,行事骄横无比。

Posted via email from purl's posterous

August 18, 2012 04:31 AM

August 17, 2012

John 18:36

purl in your heart

http://bible.us/John18.36.CCB 耶稣答道:“我的国不属于这个世界,如果我的国属于这个世界,我的臣仆早就起来争战了,我也不会被交在犹太人的手里。但我的国不属于这个世界。”

Posted via email from purl's posterous

August 17, 2012 10:56 PM

July 03, 2012

真理必叫你们得到自由

purl in your heart

The Truth Sets You Free

by Jon Walker

Then you will know the truth, and the truth will set you free.” (John 8:32 NIV)

你们必认识真理,真理必叫你们得到自由。 约翰福音 8:32 CCB

Read this as a prayer today:

请你今天用祷告的方式来读:

Jesus, you are the Truth, and the Truth sets me free. You are the Truth, and your Truth lives in me.

耶稣,你是真理,而真理必要叫我得到自由。你是真理,而且你的真理就住在我的里面。

I clothe myself in your Truth, Jesus, putting on the coat of a new self, “created to be like God in true righteousness and holiness” (Ephesians 4:24 NIV). You are “the way and the truth and the life,” and I am connected to God through you and you alone (John 14:6 NIV). Because of you, I know the Truth about who I am and I know that your life is working in me (Galatians 2:20).

耶稣,我让自己披戴你的真理,穿上照着上帝形象所造的新人。这新人有从真理而来的公义和圣洁。(以弗所书 4:24)。你就是道路、真理、生命,而且我是靠你,也只能靠你,才能和上帝相连(约翰福音 14:6)。因为你,我知道关于自己是谁的真相。我知道现在是你活在我的里面(加拉太书 2:20)。

Truth enables me to discern and reject the lies of the enemy. Your Truth demolishes arguments and pretensions that are at war with the knowledge of God. In your Truth, I will “take captive every thought” and make it obedient to you (2 Corinthians 10:5 NIV). With your Truth, I will teach these thoughts to obey you, King Jesus (suggested by 2 Corinthians 10:5 MSG).

真理使我能明辨是非,拒绝仇敌的谎言。你的真理使那些阻碍人认识上帝的谬论和高傲言论都被击破。在你的真理中,我能夺回被掳去的心思意念,使其顺服基督(哥林多后书 10:5 CCB)。在你的真理中,我要教导那些心思意念都去顺服你,认识你是主耶稣。

Your Truth says I’m forgiven, I’m loved by my Creator, and I’m God’s child, beloved and empowered by the Spirit of Truth.

你的真理告诉我,自己是被赦免的,我是得造物主喜悦的,是神的爱子,也充满了圣灵的大能。

The Truth enables me to respond to your direction. I can trust your leadership, I can trust your commands, and I can trust your plans because you are the Truth.

这真理让我回应你的指引。我也能相信你的带领,我能信靠你的话语,我能坚信你的计划,因为你就是真理。

I will leave it up to you to interpret the facts and determine the truth of any situation. I will let your Truth make the decisions, and then I will obey and leave the consequences in your loving, truth-filled hands.

我会让你来告诉我事实真相是如何的,带我在每个状况中明辨是非。我会让你的真理替我做决定,然后我还要遵行这个决定,从而把自己放在你的慈爱和信实中。

All the decisions are yours to make. Because you are Truth, I can respond in the Truth in all situations; I am no longer a slave to my emotions.

所有的决定都是出于你。因为你是真理,我也决心在每个状况中以真理回应你,我不再是情绪的奴隶。

 

Jon Walker is managing editor of Rick Warren’s Daily Hope Devotionals. He is also the author of Costly Grace: A Contemporary View of BonhoeffersThe Cost of Discipleship and In Visible Fellowship: A Contemporary View of Bonhoeffer's Classic WorkLife Together.

This devotional © Copyright 2012 Jon Walker. All rights reserved. Used by permission.

Posted via email from purl's posterous

July 03, 2012 09:31 PM

真理必叫你们得到自由

The Truth Sets You Free

by Jon Walker

Then you will know the truth, and the truth will set you free.” (John 8:32 NIV)

你们必认识真理,真理必叫你们得到自由。 约翰福音 8:32 CCB

Read this as a prayer today:

请你今天用祷告的方式来读:

Jesus, you are the Truth, and the Truth sets me free. You are the Truth, and your Truth lives in me.

耶稣,你是真理,而真理必要叫我得到自由。你是真理,而且你的真理就住在我的里面。

I clothe myself in your Truth, Jesus, putting on the coat of a new self, “created to be like God in true righteousness and holiness” (Ephesians 4:24 NIV). You are “the way and the truth and the life,” and I am connected to God through you and you alone (John 14:6 NIV). Because of you, I know the Truth about who I am and I know that your life is working in me (Galatians 2:20).

耶稣,我让自己披戴你的真理,穿上照着上帝形象所造的新人。这新人有从真理而来的公义和圣洁。(以弗所书 4:24)。你就是道路、真理、生命,而且我是靠你,也只能靠你,才能和上帝相连(约翰福音 14:6)。因为你,我知道关于自己是谁的真相。我知道现在是你活在我的里面(加拉太书 2:20)。

Truth enables me to discern and reject the lies of the enemy. Your Truth demolishes arguments and pretensions that are at war with the knowledge of God. In your Truth, I will “take captive every thought” and make it obedient to you (2 Corinthians 10:5 NIV). With your Truth, I will teach these thoughts to obey you, King Jesus (suggested by 2 Corinthians 10:5 MSG).

真理使我能明辨是非,拒绝仇敌的谎言。你的真理使那些阻碍人认识上帝的谬论和高傲言论都被击破。在你的真理中,我能夺回被掳去的心思意念,使其顺服基督(哥林多后书 10:5 CCB)。在你的真理中,我要教导那些心思意念都去顺服你,认识你是主耶稣。

Your Truth says I’m forgiven, I’m loved by my Creator, and I’m God’s child, beloved and empowered by the Spirit of Truth.

你的真理告诉我,自己是被赦免的,我是得造物主喜悦的,是神的爱子,也充满了圣灵的大能。

The Truth enables me to respond to your direction. I can trust your leadership, I can trust your commands, and I can trust your plans because you are the Truth.

这真理让我回应你的指引。我也能相信你的带领,我能信靠你的话语,我能坚信你的计划,因为你就是真理。

I will leave it up to you to interpret the facts and determine the truth of any situation. I will let your Truth make the decisions, and then I will obey and leave the consequences in your loving, truth-filled hands.

我会让你来告诉我事实真相是如何的,带我在每个状况中明辨是非。我会让你的真理替我做决定,然后我还要遵行这个决定,从而把自己放在你的慈爱和信实中。

All the decisions are yours to make. Because you are Truth, I can respond in the Truth in all situations; I am no longer a slave to my emotions.

所有的决定都是出于你。因为你是真理,我也决心在每个状况中以真理回应你,我不再是情绪的奴隶。

 

Jon Walker is managing editor of Rick Warren’s Daily Hope Devotionals. He is also the author of Costly Grace: A Contemporary View of BonhoeffersThe Cost of Discipleship and In Visible Fellowship: A Contemporary View of Bonhoeffer's Classic WorkLife Together.

This devotional © Copyright 2012 Jon Walker. All rights reserved. Used by permission.

Posted via email from purl's posterous

July 03, 2012 07:14 PM

April 29, 2012

Untitled

purl in your heart

April 29, 2012 06:41 AM

April 25, 2012

MongoDB and Perl

Fayland And Programming

it's really a pain to work with MongoDB in Perl. Perl has no 'type' so when you get INT value from DBI, it might be really "1" instead of int 1.


I got some data dumped from MySQL to MongoDB and found all the 'time' field is wrapped as "1335350669" instead of 1335350669.

and when you insert it from code like { time => time() }, you really have 1335350670 instead of "1335350670". it breaks the sort. it breaks the deletion. it breaks everything.

blabla, to fix that, we just need update it in mongodb like below.

PRIMARY> db.jobs.find().forEach(
    function(job) {
        job.time = parseInt(job.time);
        db.jobs.save(job);
    });

but it's still a pain. I didn't check MongoDBx::Class or Mongoose yet, but Moose's type should be able to fix it I think.

Thanks

April 25, 2012 04:00 PM

April 13, 2012

api and android app

Fayland And Programming

I'm a fan of Android. even I like my iPad and MBP very much too.


I'd like to write an App for Android. and here is all the story.

first of all, I need write an API which supports JSONP. since it would be very simple, I don't want to mix it up with Dancer. at last, I wrote something based on webmachine-perl. I like the idea behind the webmachine. the chain design looks pretty nice. even I don't use that too much.

the code is at

https://github.com/fayland/findmjob.com/blob/master/api/app.psgi
https://github.com/fayland/findmjob.com/blob/master/api/lib/FindmJob/Resource.pm

and live demo as

http://api.findmjob.com/search?callback=jQuery171016797792096622288_1334322201618&q=perl&loc=&_=1334322214418

after API is done. now comes the android app part. Sorry that I don't know much about Java. and I failed to download appmobi/jqmobi (the download never ends here). so at last I picked up phonegap.

the progress went pretty smooth. mixed with jQuery Mobile, I have it out after few hours.

you can download it for fun from http://static.findmjob.com/FindmJob.apk
it's pretty simple, just with one JSONP request and not much different than the demo.

even there, I'd like to share the code with you:

https://github.com/fayland/findmjob.com/blob/master/mobile/android/assets/www/index.html
https://github.com/fayland/findmjob.com/blob/master/mobile/android/assets/www/mobile.coffee

it's very cool and I'm excited! (badly Google costs 25$ for submitting it and I don't want to do it for now)

Thanks.



April 13, 2012 04:00 PM

April 07, 2012

sphinx search with varchar primary key

Fayland And Programming

usually when you index the mysql data into sphinx, you'll use id int/bigint for the primary key. but it's broken for me on http://findmjob.com/, we use uuid everywhere for the primary key.


here is the solution for it. use @id := @id + 1 for the indexer, and use sql_field_string to get the real id when matched. sample code below:

sql_query_pre = SET NAMES utf8
sql_query_pre = SET @id := 1;
sql_query = \
    SELECT @id := @id + 1 AS tid, id, title, description, location, contact, inserted_at FROM job ORDER BY inserted_at DESC LIMIT 10000

sql_field_string = id

it requires latest sphinx to support sql_field_string. and the latest CPAN module too.

and Perl code for it will be normal like before. and instead you use ->{doc}, you need use the attribtues ->{id} like

my @jobids = map { $_->{id} } @{$ret->{matches}};

so now we have sphinx search supports in my new site, eg: http://findmjob.com/search/Perl.html?q=Perl

Thanks.

April 07, 2012 04:00 PM

April 05, 2012

Wildlife park, Beijing

purl in your heart

pdfcreatorultimatefree_20120406_4.pdf Download this file

Posted via email from purl's posterous

April 05, 2012 02:11 PM

cidr2regexp.pl

$ cidr2regexp.pl 210.212.0.0/11

210\.((19[2-9])|(20[0-9])|(21[0-9])|(22[0-3]))

$ cidr2regexp.pl 210.212.0.0/12

210\.((20[8-9])|(21[0-9])|(22[0-3]))

$ cidr2regexp.pl 210.212.0.0/13

210\.((20[8-9])|(21[0-5]))

#!/usr/bin/perl -l

BEGIN{ ($ip,$bits)=split q(\/), shift }

$bin_str=substr( (join qq(), map { sprintf q(%08b), $_ } split (q(\.), $ip)), 0, $bits);

push @eight_bins, $1 while $bin_str=~m{(.{1,8})}g;

@ddd = map {

  if (length($_) == 8) { eval qq(0b$_) }

  else {

    qq(@{[eval qq(0b@{[substr($_.q(0)x8,0,8)]})]} .. @{[eval qq(0b@{[substr($_.q(1)x8,0,8)]})]})

  }

} @eight_bins;

END {

  do { @digits=split q(), $_; $seen{join q(), @digits[0..$#digits-1]}.=$digits[-1] }

    for eval qq( @{[grep { m{\.\.} }  @ddd]} );

  print join qq(\\\.), (grep { !m{\.\.} } @ddd ),

    eval {

      q{(}.

      (

         join qq(\|),

           map {qq(($_\[@{[substr($seen{$_},0,1)]}-@{[substr($seen{$_},-1,1)]}]))}

             sort {$a<=>$b} keys %seen

      )

      .q{)}

      if $ddd[-1]=~m{\.\.}

    }

}

Posted via email from purl's posterous

April 05, 2012 05:27 AM

April 03, 2012

DBIx::Class with Moose has

Fayland And Programming

well, I don't know how to name it.


DBIx::Class is one of my favorite modules. for its structure, with DBIx::Class you can make all your code very well organized and clean. writing code with DBIx::Class means you can use it in framework like Catalyst or Mojo or Dancer, and you can use it in any perl script (cron usually). for me, DBIx::Class is the right model, TT2 is the right template, and framework is just for URL dispatch.

Moose is another module I like. Role, and clean OO.

by using DBIx::Class::Schema::Loader make_schema_at, with use_moose => 1 on (eg: https://github.com/fayland/findmjob.com/blob/master/script/make_schema_at.pl), we can generate very clean Result module with Moose.

sometimes you want make assessor based on the table column. like for each job in table, we all have a URL which is based on the id and title in the table, it's so related to those two fields, so we'd better to put it in the Result.
with DBIx::Class, it's very simple. (sample code)

package FindmJob::Schema::Result::Job;

....

use FindmJob::Utils 'seo_title';
has 'url' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
sub _build_url {
    my ($self) = @_;

    return "/job/" . $self->id . "/" . seo_title($self->title) . ".html";
}

after that, we can always call $job->url after we ->search for ->find it. very neat. live demo like: 

http://findmjob.com/job/Il2bUdB74RGuDaqxKQ5yzw/Senior-Perl-Developer.html

have fun. Thanks

April 03, 2012 04:00 PM

April 01, 2012

findmjob.com

Fayland And Programming

maybe to earn some money (not for fun this time), I decided to write a new website http://findmjob.com/


I sit front of my computer and coded it for 2 days and here is it. it's out.

it's very simple and without much stuff yet. and the final goal is undecided. but there it is. I'm very pleased to see it in public.

since there is no reason to keep it private, I opened source it in github: https://github.com/fayland/findmjob.com

for programmer, it's very simple to write website. but it's very hard to make it a success. so suggestions are welcome.

Thanks.

April 01, 2012 04:00 PM

better pagination url design in Dancer

usually People do param for pager like ?page=1 or ?p=1, it maybe not that good for search engine because they may not go scrape inside. so we may come out a solution with /page=1/ or /p=1/ or even /p.1/ etc.


in Dancer, it's very tricky to do add pagination regex in all URLs. and thank God, we have 'forward' and with code like below, it becomes very simple and easy to use.

Perl code:
get qr'.*?/p\.(\d+).*?' => sub {
    my $uri = request->uri;
    $uri =~ s'/p\.(\d+)'';
    var page => $1;
    $uri =~ s/\/$//;
    forward $uri;
};

get '/' => sub {
    my $p = vars->{page} || 1; $p = 1 unless $p =~ /^\d+$/;

we use regex to get the page stuff, then remove it from the request uri then using forward to do a internal request.

the TT2 pager code can check at https://github.com/fayland/findmjob.com/blob/master/templates/pager.tt

live demo as: http://findmjob.com/tag/perl/ and http://findmjob.com/tag/perl/p.2/

have fun.

April 01, 2012 04:00 PM

March 29, 2012

Net-GitHub-0.43_01

Fayland And Programming

Github is moving on with their API that "We will terminate API v1 and API v2 in 1 month on May 1st, 2012.".


I know lots of people are preferring that they want token instead of writing user/pass in the config or code.
and now we have the choice with create access_token with code instead of web flow.

it's time to kick Net::GitHub default to V3 now. so here comes the beta release. and tests/patches are welcome.

http://fayland.org/CPAN/Net-GitHub-0.43_01.tar.gz (it will be on CPAN soon too)

Tips to create access_token with script:

use Net::GitHub;
my $gh = Net::GitHub->new( login => 'fayland', pass => 'secret' );
my $oauth = $gh->oauth;
my $o = $oauth->create_authorization( {
    scopes => ['user', 'public_repo', 'repo', 'gist'], # just ['public_repo']
    note   => 'test purpose',
} );
print $o->{token};

record the token down, and later on we can always use that token without writing down user/pass.

my $github = Net::GitHub->new(
    access_token => $token, # from above
);

Thanks.

March 29, 2012 04:00 PM

March 16, 2012

Heartfelt man

purl in your heart

March 16, 2012 01:50 AM

February 26, 2012

在 神我们的父面前,那清洁没有玷污的虔诚,就是看顾在患难中的孤儿寡妇,并且保守自己不沾染世俗。 http://bible.us/jas1.27.cunpss

purl in your heart

February 26, 2012 06:39 AM

January 19, 2012

Split inside specific column and expend to key-value extractor

purl in your heart

col2kv ()
{
    perl -F\\t -lane 'BEGIN{ $i=shift; @h=split q(,), shift; print STDERR join qq(\t), @h } %k=map { @{[split(q(:), $_)]}[0,1] } split q(, ), $F[$i]; print join qq(\t), map { $k{$_} } @h' $*
}

$ echo 2010Q1:H, 2007:Y, 2009:Y| col2kv 0 2006,2007,2008,2009,2010Q1,2010Q2 2>&1| row2col
2006
2007    Y
2008
2009    Y
2010Q1  H
2010Q2

Posted via email from purl's posterous

January 19, 2012 02:03 AM

January 06, 2012

How to make group_by_in_perl?

purl in your heart

$ typeset -f group_by_in_perl
group_by_in_perl ()
{
    perl -F\\t -lane 'BEGIN{$group_by=shift; $sum_by=shift} END { print for map{ join qq(\t), $_, $sum{$_}} keys %sum } $sum{join qq(\t), @F[eval($group_by)]} += $F[eval($sum_by)]' $*
}

Posted via email from purl's posterous

January 06, 2012 10:42 PM

BJ in snowing

January 06, 2012 08:54 PM

December 27, 2011

xml-table-maker for Windows

purl in your heart

@echo off
perl -e "use Win32::Clipboard; use DBIx::XHTML_Table; Win32::Clipboard::Set(DBIx::XHTML_Table->new(q(dbi:Oracle),qq(@ARGV))->exec_query(eval <STDIN>)->modify(table=>{border=>1, bordercolor=> q(#888888), cellspacing=>0})->output())"

Posted via email from purl's posterous

December 27, 2011 01:22 AM

December 03, 2011

Plack::Middleware::FileWrap

Fayland And Programming

When you really go coding, you'll meet lots of issues. then you'll write solution for them. that's straight.

Today I have another CPAN module Plack::Middleware::FileWrap out to fit my demand: I'll have lots of plain HTML files, they'll share the same header/footer and I don't want to use stupid iframe.

under Plack, it just means wrap $res->[2] with file content or strings. 

so here comes Plack::Middleware::FileWrap, very simple if you looked at the source code.

then I used it in the KinderGarden project, with snippets:

    mount '/static/docs/' => builder {
        enable 'FileWrap', headers => ["$root/static/docs/header.html"], footers => ["$root/static/docs/footer.html"];
        Plack::App::File->new( root => "$root/static/docs" )->to_app;
    },
    
Live demo as http://kindergarden.fayland.org/static/docs/TestLocally.html

now all the files under static/docs will wrapped with header.html and footer.html (header.html/footer.html itself too!)

for some advanced example, like if you just want to apply to html files, then you can code something like:

    mount '/static/docs/' => builder {
        enable_if { $_[0]->{PATH_INFO} =~ /\.html/ } 'FileWrap',
            headers => ["$root/static/docs/header.html"], footers => ["$root/static/docs/footer.html"];
        Plack::App::File->new( root => "$root/static/docs" )->to_app;
    },
    
I didn't put the Content-Type check b/c I think it's better to be handled with enable_if.

Enjoy. Thanks

December 03, 2011 04:00 PM

December 02, 2011

git submodule

Fayland And Programming

When you include another open source in your own project, it's usually pretty hard to keep it up to date. it becomes even more harder if you have some modification on it.


recently I have bootstrap in my KinderGarden project, it's very easy to use git submodule to handle it.

kindergarden> git submodule add https://github.com/twitter/bootstrap.git static/bootstrap
kindergarden> git add .gitmodules static/bootstrap
kindergarden> git commit -a -m "remote bootstrap"
kindergarden> git push
kindergarden> git submodule init

in delopy or other machine:

kindergarden$ git submodule init
kindergarden$ git submodule update

pretty easy and simple, and the logic behind is simple too. reference as http://help.github.com/submodules/ or http://progit.org/book/ch6-6.html

BTW, I added Live.com OAuth2 supports to KinderGarden.

Enjoy. Thanks

December 02, 2011 04:00 PM

November 30, 2011

2011 CN Perl Advent

Fayland And Programming

Hi, it's time for advent again!


and here it is: http://perlchina.github.com/advent.perlchina.org/

the repos is at https://github.com/PerlChina/advent.perlchina.org

Enjoy!

November 30, 2011 04:00 PM

November 29, 2011

我们为你们所存的盼望是确定的

purl in your heart

November 29, 2011 08:20 PM

PDC: 圣经说,爱是个习惯

Tuesday, November 29, 2011

   
Image002

The Bible Says Love Is a Habit 圣经说,爱是个习惯
by Rick Warren

“If you love those who love you, what credit is that to you? Even sinners love those who love them.” (Luke 6:32 NIV)

你们若单爱那爱你们的人,有什么可酬谢的呢?就是罪人也爱那爱他们的人。路加福音 6:32

If you only love on and off like a light switch, you do not love others like God wants you to love. Jesus said, “If you only love those who love you, what credit is that to you?” (Luke 6:32a NIV)

如果你的爱像电灯一样时开时关,那么你就没有按照神的心意去(活出�的)爱了。所以,耶稣这样说:你们若单爱那爱你们的人,有什么可酬谢的呢?(路加福音 6:32

His point is this: All of us can love those who love us back. Becoming a master lover means you learn to love the unlovable � when you love people who don’t love you, when you love people who irritate you, when you love people who stab you in the back or gossip about you.

他的意思是:我们每个人都能做的,就是爱那些知恩图报的人。而你若想要成为一个有博爱之心的人,就得学着去爱那些不可爱的人。也就是,去爱那些不爱你的人,包括那些常常触怒你的人,或是那些在你背后指指点点、说长道短的人。

This may seem like an impossible task, and it is � that’s why we need God’s love in us, so we can then love others: “We know and rely on the love God has for us” (1 John 4:16a NIV).

如果这听上去有点象天方夜谭,那你其实是清醒的。因为,无私的付出爱,并且一味的坚持,这真的需要神的爱先充满我们的心。所以,圣经这样说:神爱我们的心,我们也知道也信(约翰一书 4:16)。

When you realize how much God loves you � with an extravagant, irresistible, unconditional love � then his love will change your entire focus on life. If we don’t receive God’s love for us, we’ll have a hard time loving other people. I’m talking about loving people who are unlovely, difficult, irritable, and those who are different or demanding.

当你认识到神对自己的爱有多么丰盛、多么的坚忍、多么的无私,那么�的爱就能改变你对生命的关注点。如果我们不去接受神给我们的爱,那么关爱他人就是一件太难太难的事。注意,这里我们说的仍然是爱那些不可爱的、满是困难的、易于激怒人的、与常人迥异的、常常不满足的人。

You can’t do that until you have God’s love coming through you. You need to know God’s love so it can overflow out of your life into others.

没有神的爱在你心里运行,这些就真的太难了。你必须去认识神的爱,这样你的心里才能充满�的爱,直到这爱开始满溢,涌流出来,进入他人的生命中。

Posted via email from purl's posterous

November 29, 2011 06:52 PM

November 26, 2011

KinderGarden

Fayland And Programming

as talked yesterday, I get it uploaded into github. well, under PerlChina. https://github.com/PerlChina/kindergarden


I really want to draw more people to add more features and fix more bugs.

and I added one new feature with Mojolicious which is http://kindergarden.fayland.org/app/whereilive

feel free to view the source and let me know what you think!

Thanks.

November 26, 2011 04:00 PM

November 25, 2011

Dancer::Template::Xslate

Fayland And Programming

I'm writing some toy once again with Plack and Dancer (and Mojo later).


this time, I'm playing Plack::Middleware::OAuth and Dancer::Template::Xslate a bit. the website is http://kindergarden.fayland.org/ and I'll open source it if someone is interested. for now, it's just 'Login with ...' OAuth and nothing else. (layout is built with twitter bootstrap)

the Dancer::Template::Xslate has some bugs and I tried to submit few commits through github to fix it. (at least it's working for me now)

here is a tip to add function like gravatar into Xslate within Dancer.

problem as Text::Xslate supports function param when ->new but Dancer YAML config can't have Perl code inside. and it's very tricky or hard to fix the engine 'template' b/c we can never modify it. it has 'my $_engine;' inside code and you can't modify it at all.

after a while, I find a good solution with the module param of the Text::Xslate. it's very neat. sample code as below:

# config.yml
template: xslate
engines:
  xslate:
    syntax: 'TTerse'
    extension: 'tt'
    header:
      - 'layout/header.tt'
    footer:
      - 'layout/footer.tt'
    module:
      - KinderGardenX::Text::Xslate::Bridge::KinderGarden

# KinderGardenX::Text::Xslate::Bridge::KinderGarden
package KinderGardenX::Text::Xslate::Bridge::KinderGarden;

use strict;
use warnings;
use parent qw(Text::Xslate::Bridge);

use Gravatar::URL;

my %funtion_methods = (
    gravatar_url => \&gravatar_url,
);

__PACKAGE__->bridge(
    function => \%funtion_methods,
);

1;

# template

    <img src="[% gravatar_url( email => user.email, size => 30) %]" />
    <img src="[% gravatar_url( email => user.email, size => 50) %]" />
    <img src="[% gravatar_url( email => user.email) %]" />

Note there is always more than one way to do it. 

Thanks.

November 25, 2011 04:00 PM

November 07, 2011

new baby

Fayland And Programming

I'm very happy to share the good news with all the world. my second kid, another boy, was born today. 9:45am Beijing Time, Nov 8th, 2011. 2800g. and everything is good. Thanks.

November 07, 2011 04:00 PM

November 06, 2011

Psalm 16:7

purl in your heart

I praise you, Lord , for being my guide. Even in the darkest night, your teachings fill my mind.

http://bible.us/Ps16.7.CEV

Posted via email from purl's posterous

November 06, 2011 05:31 PM

October 25, 2011

po4a for the translation of Perldoc

purl in your heart

To make a translation of perldoc, use the tool named po4a

[jjiang@fedora14 ~]$ pmvers Locale::Po4a::TransTractor

0.41

[jjiang@fedora14 ~]$ po4a-gettextize --help-format

List of valid formats:

  - dia: uncompressed Dia diagrams.

  - docbook: DocBook XML.

  - guide: Gentoo Linux's XML documentation format.

  - ini: INI format.

  - kernelhelp: Help messages of each kernel compilation option.

  - latex: LaTeX format.

  - man: Good old manual page format.

  - pod: Perl Online Documentation format.

  - sgml: either DebianDoc or DocBook DTD.

  - texinfo: The info page format.

  - tex: generic TeX documents (see also latex).

  - text: simple text document.

  - wml: WML documents.

  - xhtml: XHTML documents.

  - xml: generic XML documents (see also docbook).

[jjiang@fedora14 ~]$ perldoc -l perlretut

/usr/share/perl5/pod/perlretut.pod

[jjiang@fedora14 ~]$ po4a-gettextize -f pod -m $(perldoc -l perlretut) | tee perlretut.po | wc -l

5155

[jjiang@fedora14 ~]$ vim perlretut.po

#. type: =head1

#: /usr/share/perl5/pod/perlretut.pod:1

msgid "NAME"

msgstr "名称"

#. type: textblock

#: /usr/share/perl5/pod/perlretut.pod:3

msgid "perlretut - Perl regular expressions tutorial"

msgstr "perlretut - Perl 正则表达式指南"

#. type: =head1

#: /usr/share/perl5/pod/perlretut.pod:5

msgid "DESCRIPTION"

msgstr "简介"

#. type: textblock

#: /usr/share/perl5/pod/perlretut.pod:7

msgid ""

"This page provides a basic tutorial on understanding, creating and using "

"regular expressions in Perl.  It serves as a complement to the reference "

"page on regular expressions L<perlre>.  Regular expressions are an integral "

"part of the C<m//>, C<s///>, C<qr//> and C<split> operators and so this "

"tutorial also overlaps with L<perlop/\"Regexp Quote-Like Operators\"> and "

"L<perlfunc/split>."

msgstr ""

"这篇文章用来介绍 Perl 正则表达式的解读、编写和使用方面的基础知识。相对于 L<perlre> 中的介绍来说,这篇文章更加侧重于提供一些增补知识。正则表达式,它是 C<m//>, C<s///>, C<qr//> C<split> 这些操作符的主要兴趣所在,因此L<perlop/\"Regexp Quote-Like Operators\"> L<perlfunc/split> 里面也有许多相关的描述。"

[jjiang@fedora14 ~]$ po4a-translate -k 0 -f pod -m $(perldoc -l perlretut) -p perlretut.po | less

Image001

Posted via email from purl's posterous

October 25, 2011 11:55 PM

October 20, 2011

Non-stop debugging of perl programs

purl in your heart

$ PERLDB_OPTS="NonStop frame=31" perl -dle 'sub abc { return $_[0] + 1 } print join qq(\t), abc(0), abc(1+2)'
Package -e.
in  @=main::abc(0) from -e:1
out @=main::abc(0) from -e:1
list context return from main::abc:
0  1
in  @=main::abc(3) from -e:1
out @=main::abc(3) from -e:1
list context return from main::abc:
0  4
1       4

Posted via email from purl's posterous

October 20, 2011 11:13 PM

October 19, 2011

PDC:慷慨也是信心的表现

purl in your heart

Wednesday, October 19, 2011

   
Image002

Generosity is a Matter of Faith慷慨也是信心的表现
by Rick Warren

A generous man will prosper and he who refreshes others will himself be refreshed. Proverbs 11:25 (NIV)
好施舍的,必得丰裕; 滋润人的,必得滋润。 箴言 11:25

When you share with others, God shares with you.

当你与他人分享的时候,神也会与你分享。

The world says, “Get everything you can and you will be financially secure.” The Bible says share with others in need and you’ll sow what you reap: “Give and it will be given to you.” (Luke 6:38 NIV)

这个世界的逻辑是尽可能的攫取,这样你就会富有,而圣经的原则是要尽可能的与他人分享,这样你就会有丰厚的回报:你们要给人,就必有给你们的”(路加福音 6:38)

God says that when you give to somebody else, you're not throwing it away. It’s an investment in the lives of others. God says the one who gives will gain even more: “He who is kind to the poor, lends to the Lord and He will reward him for what he has done.” (Proverbs 19:17 NIV)

在神的眼中,当你给予别人的时候,并不是在舍弃什么,而是对其他人的生命进行投资。神会对那些慷慨付出的人给予更多回报:“怜悯贫穷的,就是借给耶和华。他的善行,耶和华必偿还”(箴言 19:17

When you see people in need and you give to them, God looks at this as if it were a loan to Him. He says, “I will reward back.”

所以,当你向那些需要的人伸出援手的时候,神会把这看成是对�自己的一次借贷。�就这样想:“我必须偿还他”。

God is always going to take care of you and your needs. Do you believe that is true? Generosity is a matter of faith. Will you take God at his Word?

神总是想要帮助你,满足你的需要。你相信这个道理么?慷慨也是信心的一种表现。你要不要在这个方面顺服神的吩咐呢?

Posted via email from purl's posterous

October 19, 2011 06:41 PM

October 12, 2011

remove/add job to crontab by commandline

Fayland And Programming

1. add job to crontab


(crontab -u fayland -l ; echo "*/5 * * * * perl /home/fayland/test.pl") | crontab -u fayland -

2. remove job from crontab

crontab -u fayland -l | grep -v 'perl /home/fayland/test.pl'  | crontab -u fayland -

3. remove all crontab

crontab -r

nothing is tricky. expect it took me 10 minutes to figure out '-' is the one I want. (- is STDOUT in Linux).

Thanks.

October 12, 2011 04:00 PM

October 11, 2011

Psalm 118:24

purl in your heart

 这是耶和华所定的日子, 我们在其中要高兴欢喜!

http://bible.us/Ps118.24.CUNPSS

Posted via email from purl's posterous

October 11, 2011 04:31 AM

September 29, 2011

Draw the Cross in Unicode

purl in your heart

% perl -MUnicode::String=uchr -le 'print uchr(10014)'

Posted via email from purl's posterous

September 29, 2011 03:19 AM

September 28, 2011

sphinx 0.99 bug (attributes count vs fields count)

Fayland And Programming

when you have 4 columns in sql_query, and you want 3 columns as attributes. you'll get a failure. 0 size sphinx files.


it's quite annoying, and it cost me almost 4 hours to figure it out. I'm so dumb and so are you, SPHINX.

a simple solution is to add a dumb col in the SELECT of sql_query like

SELECT id, radians(longitude) as long_radians, radians(latitude) as lat_radians, 'dumb' FROM table

OK. actually 'dumb' is dumb because it takes more disk than 'a'.

for a detailed issue description, please check http://sphinxsearch.com/forum/view.html?id=8345

Thanks.

September 28, 2011 04:00 PM

September 25, 2011

Script to find the root directory usage, on system with lots of mounts

purl in your heart

sudo perl -MList::MoreUtils=any -lne 'BEGIN{@m=map {@F=split; qq(^$F[2])} map {$1 if m{(.*)}} qx{mount|tail --line=+2}; open STDIN, q(find / -maxdepth 3 -mindepth 1 |)} $p=$_; do {print join qq(\t), qx(du -s "$_")=~m{(.*)}} unless any {$p=~m{$_} or $_=~m{$p}} @m' | sort -k1 -nrg | head

Posted via email from purl's posterous

September 25, 2011 09:11 PM

September 24, 2011

Net-GitHub 0.40_02

Fayland And Programming

it's a story following the previous one. and this one will be shorter.


I got Net-GitHub 0.40_02 released few minutes ago. with
* Gists, Git Data, Orgs supports
* methods on fly

there are still something to do like Pagination and MIME-Types. but most of the functions should be working now.

big thanks to Moose team, I becomes a little smarter than yesterday.

yesterday I was dumb. I wrote every methods with sub, with arguments fix, with ->query or check DELETE status. lots of duplication codes.

I cleaned all the code up with __PACKAGE__->meta->add_method. now all the code looks very clean and easy to maintain.

old code looks like (https://github.com/fayland/perl-net-github/blob/3c7cb3393834d5dd5d5bc4b583fcb1669ef8ef2d/lib/Net/GitHub/V3/PullRequests.pm)

new code is really much better: https://github.com/fayland/perl-net-github/blob/master/lib/Net/GitHub/V3/PullRequests.pm

the main tricky here is the ->meta->add_method.

## build methods on fly
sub __build_methods {
    my $package = shift;
    my %methods = @_;
    
    foreach my $m (keys %methods) {
        my $v = $methods{$m};
        my $url = $v->{url};
        my $method = $v->{method} || 'GET';
        my $args = $v->{args} || 0; # args for ->query
        my $check_status = $v->{check_status};
        my $is_u_repo = $v->{is_u_repo}; # need auto shift u/repo
        
        $package->meta->add_method( $m => sub {
            my $self = shift;
            
            # count how much %s inside u
            my $n = 0; while ($url =~ /\%s/g) { $n++ }
            
            ## if is_u_repo, both ($user, $repo, @args) or (@args) should be supported
            if ( ($is_u_repo or index($url, '/repos/%s/%s') > -1) and @_ < $n + $args) {
                unshift @_, ($self->u, $self->repo);
            }

            # make url, replace %s with real args
            my @uargs = splice(@_, 0, $n);
            my $u = sprintf($url, @uargs);
            
            # args for json data POST
            my @qargs = $args ? splice(@_, 0, $args) : ();
            if ($check_status) { # need check Response Status
                my $old_raw_response = $self->raw_response;
                $self->raw_response(1); # need check header
                my $res = $self->query($method, $u, @qargs);
                $self->raw_response($old_raw_response);
                return index($res->header('Status'), $check_status) > -1 ? 1 : 0;
            } else {
                return $self->query($method, $u, @qargs);
            }
        } );
    }
}

next step will be Pagination and MIME-types. and later.

Thanks

September 24, 2011 04:00 PM

September 23, 2011

Net-GitHub 0.40_01

Fayland And Programming

it's a quite long story. but it's all about Net::GitHub


Github released their V3 API few months ago.

the reason why I didn't update the module is super simple, I'm kind busy recently. my wife is during pregnancy. we'll have another kid 2 months later. and I even don't use in my daily life. I wrote it because I enjoy writing stuff for people.

There is a CPAN module Pithub. it is great. even he is reinventing another wheels instead of contributing, I have to say: nice module, well written. I was thinking to add some notes in Net-GitHub to say that if you're looking for V3 implemention, please try Pithub.

I changed my idea suddenly after c9s patched the module for access_token supports. if I accept it and write POD for it, why not write V3 API too?

Writing code for public is enjoyable. you can't write messy code because people use it and rate you as dumb guy. I don't want to be dumb so I have to be smarter.

Writing code is easy and simple. The most hard part is to design the API. how it works so that user will feel comfortable to use it.

Here comes few thoughts on Net-GitHub.

1. raw query should be supported so if Github add any new API, people can at least use it without waiting for another release.

use Net::GitHub;
my $gh = Net::GitHub->new( login => 'fayland', pass => 'secret' );

my $data = $gh->query('/user');
$gh->query('PATCH', '/user', { bio => 'another Perl Programmer and Father' });
$gh->query('DELETE', '/user/emails', [ 'myemail@somewhere.com' ]);

so most of the methods is just a wrapper like:

sub emails { (shift)->query('/user/emails'); }

2. more than half of the Github API is binded with :user/:repo. but it will be really very boring to type user/repo for every call.
but for one-off call, pass user/repo should be better. so both of them should be supported.

$gh->set_default_user_repo('fayland', 'perl-net-github');
my @issues = $gh->issue->issues;
my @pulls    = $gh->pull_request->pulls;

# or one-off call
my @contributors = $gh->respo->contributors($user, $repo);

I kicked out the version to public today. but there are still a lot of stuff missing. I released it because I want to hear some feedback from the users. below are some todos.
1. Orgs, Gists, Git Data
2. Pager and MIME types
3. Moose handles like $gh->pulls = $gh->pull_request->pulls to ease keyboard.
4. method builder so there isn't too much duplication code like now.

but I may not be able to finish all of them soon. so if anyone is willing to help, please fork on https://github.com/fayland/perl-net-github and patches are welcome!

Thanks

September 23, 2011 04:00 PM

SQLite related 2 utilities, to fix the book & chapter names problems of Blackberry YouVersion bible reader

purl in your heart

SQLite.pl

#!/usr/bin/perl -w

use strict;

use DBI;

my @r;

my $d=DBI->connect(qq(dbi:SQLite:dbname=@{[shift]}), q(), q());

my $s=$d->prepare_cached(join q(),<STDIN>);

$s->execute(@ARGV);

$,=qq(\t); $\=qq(\n);

print STDERR @{$s->{NAME}}; print @r while @r=$s->fetchrow_array;

$s->finish; $d->disconnect;

Do-SQLite-for.pl

#!/usr/bin/perl -w

use strict;

use DBI;

my $d=DBI->connect(qq(dbi:SQLite:dbname=@{[shift]}), q(), q());

my $s= $d->prepare_cached(do { open(SQL, q(<), shift); join(q(),<SQL>) });

$,=qq(\t); $\=qq(\n);

while(<>) {

  chomp;

  my @F = split(qq(\t), $_, -1);

  $s->execute(@F);

}

$s->finish; $d->disconnect; close SQL;

Posted via email from purl's posterous

September 23, 2011 01:10 AM