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 04, 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 04, 2011 08:56 AM

December 03, 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 03, 2011 11:30 AM

December 01, 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!

December 01, 2011 11:17 AM

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 27, 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 27, 2011 04:46 AM

November 26, 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 26, 2011 06:10 AM

November 08, 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 08, 2011 11:50 AM

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 13, 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 13, 2011 03:35 AM

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

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 29, 2011 10:39 AM

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 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

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 25, 2011 02:09 PM

September 24, 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 24, 2011 03:38 PM

September 23, 2011

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

September 22, 2011

print raw TT2 syntax

Fayland And Programming

The tricky is TAGS: http://search.cpan.org/perldoc?Template#TAGS

[% a = 1 %]

var [% a %] blabla;

[% TAGS <$ $> %]
var [% a %] blabla

<$ TAGS [% %] $>
var [% a %] blabla;

it will print out stuff like:

var 1 blabla;
var [% a %] blabla
var 1 blabla;

simple, set tags to <$ $> and get it back to [% %]. <$ $> can be anything you like.

Thanks

September 22, 2011 03:49 AM

September 15, 2011

1John 2:28

purl in your heart

September 15, 2011 03:07 AM

September 13, 2011

Sample of XML::Dumper, working with xslt

purl in your heart

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/perldata/hashref">

<html>

<body>

  <table border="1">

   <tr>

    <th>Key</th>

    <th>Value</th>

   </tr>

    <xsl:for-each select="item">

    <tr>

     <td><xsl:value-of select="@key"/></td>

     <td><xsl:value-of select="."/></td>

    </tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

$ perl -MXML::Dumper -le 'print pl2xml(\%INC)' | xsltproc perlhash.xsl - | w3m -T text/html

Posted via email from purl's posterous

September 13, 2011 02:32 AM

September 10, 2011

pipeline for top 100 repeated word list in article/book

purl in your heart

perl -MList::MoreUtils=natatime -lane ’END{ $it  = natatime 100, sort {$cnt{$b} <=> $cnt{$a}} keys %cnt; print for map {join  qq(\t), $_, $cnt{$_}} $it->() } $cnt{$_}++ for @F

Posted via email from purl's posterous

September 10, 2011 12:35 AM

September 08, 2011

Using this to find the depending module in a quick and dirty way

purl in your heart

basedon ()
{
    pmpath $* | perl -MList::MoreUtils=uniq -lne 'BEGIN{$m=shift} print for sort map {($_)=$_=~m{(.*)}} uniq grep {!m{$m}} qx(grep -h ^use.*:: @{[s{\.pm$}{} && $_]}/*.pm)' $*
}

$ basedon XML::XSH2
use File::Spec;
use File::Temp qw(tempfile tempdir);
use Filter::Simple;
use IO::File;
use Module::Compile -base;
use Parse::RecDescent;
use Scalar::Util;
use Tie::Scalar;
use URI::file;
use XML::LibXML::Iterator;
use XML::LibXML::NodeList;
use XML::LibXML;
use base qw(XML::LibXML::Iterator);

$ basedon XML::LibXML
use IO::File;
use XML::SAX::Base;
use base qw(XML::SAX::Base);

Posted via email from purl's posterous

September 08, 2011 11:15 PM

September 07, 2011

DELETE post with Facebook::Graph

Fayland And Programming

Facebook::Graph doesn't provide DELETE method by default. but we can do it for sure. below is one sample code:


use Facebook::Graph;
use LWP::UserAgent;
use HTTP::Request::Common ();

my $fb = Facebook::Graph->new(
    app_id     => $app_id,
    secret     => $app_sec,
    postback   => $postback_url,
);

my $uri = $fb->query->find($post_id)->uri_as_string;
my $req = HTTP::Request::Common::DELETE($uri);
$req->header('Content-Length', 0);
my $response = LWP::UserAgent->new->request($req);

Note we have to set Content-Length to 0. or we'll get 400 Bad Request.

Thanks

September 07, 2011 01:52 PM

September 01, 2011

The pstree map of "zgrep abc /tmp/*.gz | wc -l"

purl in your heart

September 01, 2011 06:30 PM

August 30, 2011

get unique content from duplicated *.tar.gz contents

purl in your heart

#!/usr/bin/perl -wln

$fn = $_;

do{ @F=split; $uniq{join qq(\t), @F[3,4,2]}||= join qq(\t), $fn, $F[5] } for grep {!m{^d}map{ m{(.*)} } qx(zcat $_ | tar -tv);

END{ print join qq(\t), $_, $uniq{$_} for sort keys %uniq }

Posted via email from purl's posterous

August 30, 2011 02:33 AM

August 27, 2011

Ezekiel 3:10

purl in your heart

 Then he added, "Son of man, let all my words sink deep into your own heart first. Listen to them carefully for yourself.

http://bible.us/Ezek3.10.NLT

Posted via email from purl's posterous

August 27, 2011 07:06 AM

August 23, 2011

Mark 11:23

purl in your heart

I tell you the truth, you can say to this mountain, `May you be lifted up and thrown into the sea,' and it will happen. But you must really believe it will happen and have no doubt in your heart.

http://bible.us/Mark11.23.NLT

Posted via email from purl's posterous

August 23, 2011 04:11 PM

August 21, 2011

HTML::Table in one-liner

purl in your heart

perl -MHTML::Entities -F\\t -lane 'BEGIN{print q(<table>)} END{print q(</table>)} print qq(<tr>\t), (join q( ), map {qq(<td>@{[encode_entities($_)]}</td>)} @F), qq(\t</tr>)'

Posted via email from purl's posterous

August 21, 2011 10:20 PM

August 19, 2011

Mark 8:36

purl in your heart

 And what do you benefit if you gain the whole world but lose your own soul?

http://bible.us/Mark8.36.NLT

Posted via email from purl's posterous

August 19, 2011 04:14 AM

August 12, 2011

tips for snaked

Fayland And Programming

I'm giving snaked a try today. it's my first time to try it. and I heard it from last year's CN Perl Advent: http://advent.perlchina.org/2010/snaked.html


for those people who don't know what snaked is, snaked is like crontab and written in Perl.

so far so good. here are two cents.

1. if you put your configuration files under /home/fayland/snaked, please don't put the log there.
it you have /home/fayland/snaked/log which contains /home/fayland/snaked/snaked.log
as soon as you run snaked --configure, you'll find your snaked.log expands very fast soon with some weird log like
'new value for option snaked.log'.

that's because in snaked.pm, it will try to read all files under the snaked configure directory and load them as options.
snaked.log is a file, so the content is loaded as the value, and append to the snaked.log
so if you run 'snaked --configure' many times, you'll find the snaked.log becomes quite large with all log repeated again and again.

the fixes is to move snaked.log into another directory instead of /home/fayland/snaked

2. another tip should be simple but useful
export PS_SNAKED_CFG=/home/fayland/snaked/

then you don't need put --cfg again and again when you run snaked command.

Thanks.

August 12, 2011 02:33 PM

August 10, 2011

MySQL two tips

Fayland And Programming

that's not new and many people may already know it. but it's really very helpful when some SQL locked.

tip 1. use -A to start faster

if mysql somedatabase command didn't return mysql> for you. you really can try it with -A. that loads faster.

tip 2: set pager for large result.

mysql> pager less;

so that if you do 'SHOW FULL PROCESSLIST' that you can find the exact one you want without scrolling back or missing anything.

for sure it can be configured into ~/.my.cnf like

[mysql]
pager=less -inFX
prompt='(\u@\h) [\d]> '

Thanks for Alex who sharing it.

August 10, 2011 09:11 AM

August 02, 2011

主祷文

purl in your heart

August 02, 2011 10:08 PM

July 26, 2011

nohup-tee-tail-mail.pl

purl in your heart

#!/usr/bin/perl

use POSIX;

BEGIN{($cmd, $out, $mail, $append)=@ARGV}

my $child=fork;

die $! unless defined $child;

exit 0 if $child;

setsid;

close STDIN; #, q(<), q(/dev/null);

open STDERR, q(>&), STDERR;

open STDOUT, q(|-), qq(tee @{[$append && q(-a )]}$out|tail|mail -s $out $mail);

exec $cmd;

Posted via email from purl's posterous

July 26, 2011 10:54 PM

July 16, 2011

耶利米书 17:8

purl in your heart

他必像树栽于水旁,在河边扎根,炎热来到,并不惧怕,叶子仍必青翠,在干旱之年毫无挂虑,而且结果不止。

http://bible.us/Jer17.8.CUVS

Posted via email from purl's posterous

July 16, 2011 01:50 AM

July 11, 2011

从您的黑莓手机上获取圣经

purl in your heart

看看这款在黑莓上的免费圣经软件吧!它由YouVersion提供技术支持,有很多很酷的功能如:将录像,图片,链接和文本等网络媒体资料和任何圣经经节向关联在一起。没有广告,完全免费!刚快点击这里下载它吧!

http://www.youversion.com/download

Posted via email from purl's posterous

July 11, 2011 06:25 AM

July 10, 2011

Habakkuk 2:2

purl in your heart

?他对我说:将这默示明明地写在版上,使读的人容易读(或译:随跑随读)。

http://bible.us/Hab2.2.CUVS

Posted via email from purl's posterous

July 10, 2011 07:42 PM

July 08, 2011

Jeremiah 11:19

purl in your heart

?我却像柔顺的羊羔被牵到宰杀之地;我并不知道他们设计谋害我,说:我们把树连果子都灭了罢!将他从活人之地剪除,使他的名不再被纪念。

http://bible.us/Jer11.19.CUVS

Posted via email from purl's posterous

July 08, 2011 09:56 PM

Jeremiah 11:7

?因为我将你们列祖从埃及地领出来的那日,直到今日,都是从早起来,切切诰诫他们说:你们当听从我的话。

http://bible.us/Jer11.7.CUVS

Posted via email from purl's posterous

July 08, 2011 09:54 PM

July 01, 2011

Acts 2:28

purl in your heart

?你已将生命的道路指示我,必叫我因见你的面(或作:叫我在你面前)得着满足的快乐。

http://bible.us/Acts2.28.CUVS

Posted via email from purl's posterous

July 01, 2011 07:42 AM

John 14:13

?你们奉我的名无论求什么,我必成就,叫父因儿子得荣耀。

http://bible.us/John14.13.CUVS

Posted via email from purl's posterous

July 01, 2011 07:39 AM

June 24, 2011

Children's Day, in office dinner room

purl in your heart

June 24, 2011 05:24 PM

P&W 我心尊主为大

June 24, 2011 05:15 PM

June 23, 2011

Philemon 1:16

purl in your heart

不再是奴仆,乃是高过奴仆,是亲爱的兄弟。在我实在是如此,何况在你呢!这也不拘是按肉体说,是按主说,

http://bible.us/Phlm1.16.CUVS

Posted via email from purl's posterous

June 23, 2011 06:53 PM

Titus 3:5

他便救了我们;并不是因我们自己所行的义,乃是照他的怜悯,藉着重生的洗和圣灵的更新。

http://bible.us/Titus3.5.CUVS

Posted via email from purl's posterous

June 23, 2011 06:47 PM

Titus 3:2

不要毁谤,不要争竞,总要和平,向众人大显温柔。

http://bible.us/Titus3.2.CUVS

Posted via email from purl's posterous

June 23, 2011 06:46 PM

Proverbs 24:12

你若说:这事我未曾知道,那衡量人心的岂不明白么?保守你命的岂不知道么?他岂不按各人所行的报应各人么?

http://bible.us/Prov24.12.CUVS

Posted via email from purl's posterous

June 23, 2011 06:19 PM

June 21, 2011

Psalm 93:4

purl in your heart

耶和华在高处大有能力,胜过诸水的响声,洋海的大浪。

http://bible.us/Ps93.4.CUVS

Posted via email from purl's posterous

June 21, 2011 10:24 PM

Esther 4:14

此时你若闭口不言,犹大人必从别处得解脱,蒙拯救;你和你父家必致灭亡。焉知你得了王后的位分不是为现今的机会吗?

http://bible.us/Esth4.14.CUVS

Posted via email from purl's posterous

June 21, 2011 07:52 PM

Song To The King by Pocket full of rocks

Song To The King

I've felt the thunder
speak of Your name
And I've watched the lightning
Your glory proclaim
I've gazed upon mountains
that testify of Your fame
And I've heard the waves on the ocean
echo their refrain

This is my song, my song to the King
And I stand in awe of Your majesty
And I don't have much
but I give everything
This is my song, my song to my King

It seems all creation
is longing for You
From here to the farthest star
they're worshipping You
And who am I God
that You are mindful of me
I'm simple in heart, simple in song
I'm a small offering

(chorus:)

And all of creation hesitates now
All of the angels
stop singing now
As the King of all glory
listens to my song

(chorus) 

Posted via email from purl's posterous

June 21, 2011 06:39 AM

June 20, 2011

rotate vertical PDF 270 degrees into PNG, for the wide screen devices

purl in your heart

#!/usr/bin/perl -w

use CGI;

my $q = CGI->new;

my $f = $q->upload('upfile');

print "Content-Type: image/png\n\n";

open PDF, q(>), q(/tmp/t.pdf);

print PDF while (<$f>);

close PDF;

system qq(convert /tmp/t.pdf -rotate 270 /tmp/t.png);

print qx(cat /tmp/t.png);

Posted via email from purl's posterous

June 20, 2011 11:08 PM

Proverbs 19:27

If you stop learning, you will forget what you already know.

http://bible.us/Prov19.27.CEV

Posted via email from purl's posterous

June 20, 2011 09:02 PM

Psalm 92:2

用十弦的乐器和瑟,用琴弹幽雅的声音,早晨传扬你的慈爱;每夜传扬你的信实。这本为美事。

http://bible.us/Ps92.2.CUVS

Posted via email from purl's posterous

June 20, 2011 08:16 PM

Song of Solomon 1:4

愿你吸引我,我们就快跑跟随你。王带我进了内室,我们必因你欢喜快乐。我们要称赞你的爱情,胜似称赞美酒。他们爱你是理所当然的。

http://bible.us/Song1.4.CUVS

Posted via email from purl's posterous

June 20, 2011 07:57 PM

2 Timothy 3:11

以及我在安提阿、以哥念、路司得所遭遇的逼迫,苦难。我所忍受是何等的逼迫;但从这一切苦难中,主都把我救出来了。

http://bible.us/2Tim3.11.CUVS

Posted via email from purl's posterous

June 20, 2011 07:56 PM