memo.xight.org

Perl

2009-11-17 Tue

Template::Toolkitでutf-8を扱う

- Summary
TT で utf8 のマルチバイト文字が化ける。
Template::Provider::Encoding で解決。

- Sample Source

use strict;
use warnings;
use utf8;
 
use Template;
use Template::Provider::Encoding;
use Template::Stash::ForceUTF8;
 
my $tt = Template->new(
    LOAD_TEMPLATES => [ Template::Provider::Encoding->new() ],
    STASH          => Template::Stash::ForceUTF8->new,
);
 
$tt->process('template.tt', $vars) || die $tt->error();


- Reference
Lism.in * blog - 2007-12-13 - Template::Toolkitでutf-8を扱う
http://d.hatena.ne.jp/studio-m/20071213/1197513608

Blogo el Ermitejo - 2008-09-15 - Perlモジュール評 ~ Template ToolkitとHTML::Templateの使い分け
http://blogo.ermitejo.com/2008/09/15/apliki_ekranilon_de_sxablono_lauxcele/

Template::Provider::Encoding
Debian パッケージディレクトリ検索 (バイナリ) - libtemplate-provider-encoding-perl

Template::Toolkit

The Template::Manual::Config page describes all of the Template Toolkit
http://template-toolkit.org/docs/manual/#section_Template_Manual_Variables

Template Toolkit Manual -テンプレートツールキット和訳マニュアル-
http://www.hakoniwa.net/tt/

Template Toolkit で配列やハッシュにアクセス - プログラミングのこととか
http://d.hatena.ne.jp/tetsuarossa/20070902/p1

2009-11-10 Tue

CPANモジュールのインストール時に自動でデフォルト選択をする方法

- Summary
CPANモジュールのインストール時に yes を選択するために
Enter を連打したくない。

- 方法1: 環境変数 PERL_AUTOINSTALL

export PERL_AUTOINSTALL='--defaultdeps'



- 方法2: cpan の prerequisites_policy オプション

$ cpan
cpan> o conf prerequisites_policy follow
cpan> o conf commit
cpan> quit



follow 自動的にデフォルトを選択する
ask ユーザに尋ねる
ignore 無視する (依存モジュールのインストールを行わない)

- 方法3: yesコマンドを利用する

yes '' | cpan -i Some::Module


- Reference
CPANモジュールのインストール時に自動でデフォルト選択をする方法 - Craftworks Tech Blog - Branch
http://d.hatena.ne.jp/Craftworks/20090415/1239762931

CatalystやPlaggerのインストールでyes連打をしたくない - bokut.in
http://bokut.in/mt/2007/01/catalystplaggeryes.html

2009-10-06 Tue

Twiterm - Perl で書かれた Terminal上で動作するTwitterクライアント

- Summary
Perl で書かれた Terminal上で動作するTwitterクライアント。

- Clone URL
git://github.com/sugyan/Twiterm.git

- Reference
Terminal上で動作するTwitter閲覧ツール「Twiterm」を作った - すぎゃーんメモ
http://d.hatena.ne.jp/sugyan/20090928/1254069606

sugyan's Twiterm at master - GitHub
http://github.com/sugyan/Twiterm

2008-05-13 Tue

Unicode の16進数の数値文字参照を正規表現などで元に戻す

- Summary
� のような数値文字参照から元の文字に戻す方法.

- Encodeを使用する方法

#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use utf8;
binmode STDOUT, ":utf8";
my $a = "情報時代";
$a =~ s/&#x([0-9A-F]{4});/decode('UCS2', pack('H*', $1))/ge;
print "$a\n";


- HTML::Entitiesを使用する方法
my $a = "情報時代";
use HTML::Entities;
print HTML::Entities::decode($a), "\n";


- 正規表現を使用する方法
my $a = "情報時代";
$a =~ s/&#x([0-9A-F]{4});/chr(hex($1))/ge;
print "$a\n";


- Reference
たつをのChangeLog - 2008-05-10 - Unicode の16進数の実体参照を正規表現などで元に戻す
http://chalow.net/2008-05-10-3.html

404 Blog Not Found - 2008-05-11 - perl - 文字参照を(en|de)codeする
http://blog.livedoor.jp/dankogai/archives/51048882.html

HTML::Entities

2006-11-30 Thu

coLinux 上の Emacs の kill-ring の内容をWindowsのクリップボードと同期する

- Reference
naoyaのはてなダイアリー - coLinux 上の Emacs の kill-ring の内容をWindowsのクリップボードと同期する by Perl
http://d.hatena.ne.jp/naoya/20061125/1164466544

odz buffer - coLinux で Emacs の kill-ring の内容をWindowsのクリップボードと同期する
http://d.hatena.ne.jp/odz/20061125/1164433815

odz buffer - coLinux で Emacs の kill-ring の内容をWindowsのクリップボードと同期する #2
http://d.hatena.ne.jp/odz/20061125/1164437987

2006-11-30 Thu

String::Diff - 行内差分取得モジュール

- Summary
Perlの行内差分取得モジュール.

- 文字列同士の差分を作って変更無い場所,変更が合った場所で配列を分けて作成.

my $diff = String::Diff::diff_fully('this is Perl', 'this is Ruby');
for my $line (@{ $diff->[0] }) {
    print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# -: 'Perl'
 
for my $line (@{ $diff->[1] }) {
    print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# +: 'Ruby'


- 差分の合った部分にマークを付ける.
my $diff = String::Diff::diff('this is Perl', 'this is Ruby');
 
print "$diff->[0]\n";# this is [Perl]
print "$diff->[1]\n";# this is {Ruby}
 
my $diff = String::Diff::diff('this is Perl', 'this is Ruby',{
    remove_open => '<del>',
    remove_close => '</del>',
    append_open => '<ins>',
    append_close => '</ins>',
});
 
print "$diff->[0]\n";# this is <del>Perl</del>
print "$diff->[1]\n";# this is <ins>Ruby</ins>


- 差分同士の結果をマージして一つの文字列にする.
my $diff = String::Diff::diff_merge('this is Perl', 'this is Ruby'{
    remove_open => '<del>',
    remove_close => '</del>',
    append_open => '<ins>',
    append_close => '</ins>',
});
int "$diff\n";# this is <del>Perl</del><ins>Ruby</ins>


- 正規表現のパターンにする.
my $diff = String::Diff::diff_regexp('this is Perl', 'this is Ruby');
print "$diff\n";# this\ is\ (?:Perl|Ruby)


- 利用例
Hatena::Wiki - Kwiki mutual diffs
http://hatenawiki.blogdb.jp/?action=diff&page_name=HomePage&revision_id=2&current_revision_id=3

- Reference
YappoLogs: String::Diff - 行内差分取得モジュール
http://blog.yappo.jp/yappo/archives/000479.html

String::Diff

2006-11-25 Sat

Net::SSL::ExpireDate + Test::Base で証明書の期限切れをチェック

- Summary
Net::SSL::ExpireDateTest::Baseを用いて,SSL証明書の期限切れをチェック.

#!/usr/bin/env perl
# -*- mode: cperl; -*-
use Test::Base;
use Net::SSL::ExpireDate;
use Regexp::Common qw(net);
 
my $Check_Duration;
# $Check_Duration = '15 years';
 
plan tests => 1 * blocks;
 
run {
    my $block = shift;
    my $ed = Net::SSL::ExpireDate->new( build_arg($block->name) );
    is($ed->is_expired($Check_Duration) && $ed->expire_date->iso8601,
        undef,
        $block->name);
};
 
sub build_arg {
    my ($v) = @_;
    if ($v =~ m{^(file)://(.+)}) {
        return $1 => $2;
    } elsif ($v =~ m{^(https)://([^/]+)}) {
        return $1 => $2;
    } elsif ($v =~ m{^$RE{net}{domain}{-nospace}{-keep}$}) {
        return 'https' => $1;
    } elsif (-r $v) {
        return 'file' => $v;
    } else {
        croak "$v: assume file. but cannot read.";
    }
}
 
__END__
=== rt.cpan.org
=== www.google.com


- Reference
(ひ)メモ - Net::SSL::ExpireDate + Test::Base で証明書の期限切れをチェック
http://d.hatena.ne.jp/hirose31/20061121/1164042331

- via
オレンジニュース - 2006-11-21
http://secure.ddo.jp/~kaku/tdiary/20061121.html#p10

2006-11-21 Tue

Lingua::JA::Summarize::Extract を利用した日本語文章の要約

- Summary

use strict;
use warnings;
use utf8;
use Lingua::JA::Summarize::Extract;
 
my $extracter = Lingua::JA::Summarize::Extract->new;
my $text = "日本語の文章を沢山書きます";
my $result = $extracter->extract($text);
my $summary = $result->as_string;
 
utf8::encode($summary);
print $summary; #サマリーが出てくる.


- Reference
Lingua::JA::Summarize::Extract

YappoLogs - 2006-11-13 - Lingua::JA::Summarize::Extract - 日本語文章のサマリ抽出
http://blog.yappo.jp/yappo/archives/000473.html

無駄に長いテキストを要約する
http://stone.dialog.jp/archives/extract/index.cgi

Voice of Stone #1469 無駄に長い文章を要約するツール
http://stone.dialog.jp/voice/view/1469

2006-11-18 Sat

日本の総理大臣の演説をタグクラウド化

- Summary
1. HTML::TreeBuilder で as_text
2. Text::MeCab で形態素解析
3. 名詞の頻度データを YAML に変換
4. HTML::TagCloud でタグクラウド化

- Reference
Japanese Prime Minister Speeches Tag Cloud - 日本の首相演説のタグクラウド
http://blog.bulknews.net/PMTagCloud/

日本の総理大臣の演説をタグクラウド化: blog.bulknews.net
http://blog.bulknews.net/mt/archives/002078.html

HTML::TreeBuilder
Text::MeCab
YAML
HTML::TagCloud

2006-09-15 Fri

Term::TtyRec - Perlでttyrec

- Summary
Perlからttyrecを扱うモジュール.

- SYNOPSIS

use Term::TtyRec::Player;
use FileHandle;
 
# $handle is any IO::* object
my $handle = FileHandle->new('file.tty');
my $player = Term::TtyRec::Player->new($handle);
 
# options can be set as hashref
my $player = Term::TtyRec::Player->new($handle, {
    speed => 1, nowait => undef,
});


- Reference
Term-TtyRec

GraphicWizardsLair - 2006-09-12 - コンソールをネット越しにライブで見せたいのであればscreen -xじゃなくてttyrecを使った方が簡単
http://www.otsune.com/diary/2006/09/12/1.html#200609121

ttyrec: ttyレコーダー
http://0xcc.net/ttyrec/

- via
www.textfile.org - 2006-09-13
http://d.hatena.ne.jp/textfile/20060913/tty