<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja">
<title>PC Memorandom of augustus</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/" />
<modified>2010-01-05T01:58:42Z</modified>
<tagline></tagline>
<id>tag:www.augustus.to,2010:/blog/3128//1</id>
<generator url="http://www.movabletype.org/" version="3.17-ja">Movable Type</generator>
<copyright>Copyright (c) 2010, augustus</copyright>
<entry>
<title>パスワード付きのAccessのmdbファイルの処理</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2010/01/accessmdb.html" />
<modified>2010-01-05T01:58:42Z</modified>
<issued>2010-01-05T01:48:42Z</issued>
<id>tag:www.augustus.to,2010:/blog/3128//1.153</id>
<created>2010-01-05T01:48:42Z</created>
<summary type="text/plain">perlでパスワード付きのmdbファイルにアクセスしたい場合、あるいはパスワード...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[perlでパスワード付きのmdbファイルにアクセスしたい場合、あるいはパスワードを変更したい場合にどうしたらよいか調べてみた。（近年、勤務先で要求されるセキュリティのレベルが上がってきたのだ。）
<br><br>
パスワード付きのmdbファイルにアクセスするには接続文字列に<br>
Jet OLEDB:Database Password=YourPassword;<br>
と追加すれば良い。
<pre>
# パスワード付きのmdbファイルにアクセスする例
# PASSWORD というパスワードをもつ hoge.mdb に接続する。

use Win32::OLE;
my $password="PASSWORD";

# 接続文字列を作る
$connStr="Provider=Microsoft.Jet.OLEDB.4.0;";
$connStr.="Data Source=hoge.mdb;";
# 接続文字列にパスワードを追加
$connStr.="Jet OLEDB:Database Password=$password;" if($password ne "");

#アクセスのデータベースへの接続
$objDB=Win32::OLE->new("ADODB.Connection");
$objDB->Open($connStr);

#
# ここに読み書きその他の処理を書く。
#

# 接続を閉じる
$objDB->Close();
$objDB=undef();
</pre>
<br><br>

パスワード変更をしたいときは、データベースを排他モードで開いて<br>
ALTER DATABASE PASSWORD 新パスワード 旧パスワード<br>
を実行する。（空のパスワードは NULL と記述）
<pre>
#
# hoge.mdb に接続して、パスワードを変更。
#
use Win32::OLE;
$password="PASSWORD";       #変更前のパスワード
$newpassword="NEWPASSWORD"; #変更後のパスワード

# 接続文字列を作る
$connStr="Provider=Microsoft.Jet.OLEDB.4.0;";
$connStr.="Data Source=hoge.mdb;";
# 接続文字列にパスワードを追加
$connStr.="Jet OLEDB:Database Password=$password;" if($password ne "");

#アクセスのデータベースへの接続
$objDB=Win32::OLE->new("ADODB.Connection");
$objDB->{Mode}=12; # 排他モードにする。
$objDB->Open($connStr);

# パスワード変更
$newpassword="NULL" if($newpassword eq "");
$cmd=$objDB->Execute(
	"ALTER DATABASE PASSWORD $newpassword $password"
 );

# 接続を閉じる
$objDB->Close();
$objDB=undef();
</pre>
]]>

</content>
</entry>
<entry>
<title>USBメモリのシリアル番号の取得</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2009/07/usb.html" />
<modified>2009-07-10T12:51:38Z</modified>
<issued>2009-07-10T12:48:49Z</issued>
<id>tag:www.augustus.to,2009:/blog/3128//1.152</id>
<created>2009-07-10T12:48:49Z</created>
<summary type="text/plain">USBデバイスには製造元を表わすベンダーID、製品の種別を表わすプロダクトID、...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[USBデバイスには製造元を表わすベンダーID、製品の種別を表わすプロダクトID、個別の製品の固有番号である iSerialNumber と呼ばれる文字列があり、
WMI 経由でそれらは取得できる。<br/>
以下はサンプルの perl スクリプト。<br/>
<br/>

<pre style='border:solid 1px; padding:2pt; color:magenta'>
use strict;
use Win32::OLE;
use Win32;

my $strComputer=".";
my $wmi = Win32::OLE->GetObject(
  "WinMgmts:{impersonationLevel=impersonate}!//".
  "$strComputer\\root\\cimv2"
) or die;
my $colDiskDrives = $wmi->ExecQuery(
  "SELECT * FROM Win32_DiskDrive"
);
for my $disk (in $colDiskDrives) {
	next if $disk->{PNPDeviceID}!~/^usbstor/i;
	print "$disk->{PNPDeviceID}\n";
}
</pre>

<br/>
これを使うとUSBメモリを簡単な鍵のように使うことができそうだ。<br/>
１）予めユーザが使用しているUSBメモリのシリアル番号を登録しておく。<br/>
２）ログオンスクリプトで、接続しているUSBメモリのシリアル番号を読み取り、登録済みのシリアル番号が見つからなければシャットダウンする。<br/>
３）ログオンスクリプトだけでなく、一定時間ごとにチェックしてやるとさらに良いかもしれない。<br/>
]]>

</content>
</entry>
<entry>
<title>ISBNの13桁化</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2007/02/isbn13.html" />
<modified>2007-02-19T04:50:54Z</modified>
<issued>2007-02-19T09:47:49Z</issued>
<id>tag:www.augustus.to,2007:/blog/3128//1.147</id>
<created>2007-02-19T09:47:49Z</created>
<summary type="text/plain">今年（2007年）の1月から従来10桁だったISBNが13桁に拡張された。拡張の...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[今年（2007年）の1月から従来10桁だったISBNが13桁に拡張された。拡張の仕方は単純で頭に978を付け、最後の桁のチェックデジットを再計算するだけでよいらしい。
<br/><br/>

10桁のISBNと13桁のISBNを相互変換する関数が欲しくて CPAN を探すと、
Business::ISBN というぴったりのモジュールがあった。
<pre style='border:solid 1px; padding:2pt; color:magenta'>
use Business::ISBN qw( isbn_to_ean ean_to_isbn );
$isbn_10="4873113008";
$isbn_13=isbn_to_ean($isbn_10); # 旧ISBN ---> 13桁ISBN
$isbn_10=ean_to_isbn($isbn_13); # 13桁ISBN ---> 旧ISBN
</pre>
13桁のISBN は EANコードと一致するのだ。
<br/><br/>

早速、「古代ローマ/書籍案内」で使っているプラグインに組み込んで ISBN: の後の10桁の番号を 13桁化して表示するようにした。これで各ページを個別に修正しなくてもよくてラッキーだ。
<br><br>

なお、13桁化したISBNはEANコード（日本ではJANコード）と一致するので、本の裏表紙などに印刷されているバーコードのうち上の方（978から始まるもの）がそのままISBNとなっている。
]]>

</content>
</entry>
<entry>
<title>mimeTeX (WEB上での数式の表示）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/mimetex_web.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-20T01:56:19Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.81</id>
<created>2005-03-20T01:56:19Z</created>
<summary type="text/plain">WEB上で数式を表示するのは結構大変だが、この mimeTeX を使えば簡単だ。...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>WEB</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[WEB上で数式を表示するのは結構大変だが、この mimeTeX を使えば簡単だ。<br/>
mimeTeX は C言語で書かれた CGI で、引数に TeX の文法で数式を書けば、数式の GIF イメージを戻してくれる。だから、imgタグを使って数式を簡単に表示することができるのだ。<br/>
例えば、
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large x^2+y^2=1">
と表示したければ、
<div style='color:magenta'>&lt;img src="/cgi-bin/mimetex.cgi?\Large x^2+y^2=1"></div>
のように書けば良い。(/cgi-bin に mimetex.cgi があると仮定してる。)
<br/><br/>
数式の書き方は基本的に LaTeX と一緒だが、全部の機能が使えるわけではないので mimeTeX のサイト(http://www.forkosh.com/mimetex.html)を確認してもらいたい。<br/>
<br/>
mimeTeXのソースはC言語で書かれているが自分でコンパイルしなくても良いように Windows, Linux, FreeBSD 等々のバイナリも提供されている。
<br/><br/><br/>
以下に数式の例をいくつか挙げる。<br/>]]>
<![CDATA[<br/>
分数<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \frac{1}{2}+\frac{1}{3}=\frac{5}{6}">
<div style="color:magenta">
\frac{1}{2}+\frac{1}{3}=\frac{5}{6}
</div><br/>


分数の横棒が短いと思うなら分子か分母に \, を使って空白を補完すると良い。<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \frac{1}{\,2\,}+\frac{1}{\,3\,}=\frac{5}{\,6\,}">
<div style="color:magenta">
\frac{1}{\,2\,}+\frac{1}{\,3\,}=\frac{5}{\,6\,}
</div><br/>


上付文字、下付文字<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large a_{n+1}=2 a_n +2^n">
<div style="color:magenta">
a_{n+1}=2 a_n +2^n
</div><br/>


三角関数<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \tan\theta = \frac{\Large \sin\theta}{\Large \, \cos\theta \,}">
<div style="color:magenta">
\tan\theta = \frac{\Large \sin\theta}{\Large \, \cos\theta \,}
</div><br/>


対数<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \log_{10} \alpha">
<div style="color:magenta">
\log_{10} \alpha
</div><br/>


積分（上端、下端の書かれる位置が気に入らないが...）<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \int x \,dx  \;,\; \int_{-1}^{\pi} {t \sin t}  \, dt">
<div style="color:magenta">
\int x \,dx  \;,\; \int_{-1}^{\pi} {t \sin t}  \, dt
</div><br/>


行列<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \left( \begin{array}{cc} 1 & x \\ 0 & 1 \\ \end{array} \right)^2 ">
<div style="color:magenta">
\left( \begin{array}{cc} 1 & x \\ 0 & 1 \\ \end{array} \right)^2
</div><br/>


シグマ<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \sum_{k=0}^{\infty} \frac{\Large x^k}{k!}">
<div style="color:magenta">
\sum_{k=0}^{\infty} \frac{\Large x^k}{k!}
</div><br/>

極限<br/>
<img src="http://www.augustus.to/cgi-bin/mimetex.cgi?\Large \lim_{x \to \infty}2^{-x}">
<div style="color:magenta">
\lim_{x \to \infty}2^{-x}
</div><br/>]]>
</content>
</entry>
<entry>
<title>Movable Type プラグイン作成入門（５）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/movable_type_6.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-12T23:38:54Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.80</id>
<created>2005-03-12T23:38:54Z</created>
<summary type="text/plain">今度は独自のコンテナタグの作り方を見てみよう。 例） --- CSVで与えられた...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>MovableType</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[今度は独自のコンテナタグの作り方を見てみよう。<br/><br/>

例）<MTCsvLoop> --- CSVで与えられたリストでループする。
<pre>
package MT::Plugin::CsvLoop;
use strict;
use MT::Template::Context;
MT::Template::Context->add_container_tag(
            CsvLoop=>\&CsvLoop);
sub CsvLoop{
    my($ctx,$arg) = @_;
    my(@list)=split(/,/, $arg->{list});
    my $res = '';
    my $builder = $ctx->stash('builder');
    my $tokens = $ctx->stash('tokens');
    for my $i (@list) {
        $ctx->stash('csv_value', $i);
        defined(my $out=$builder->build($ctx,$tokens))
            or return $ctx->error($ctx->errstr);
        $res .= $out;
    }
    $res;
}

MT::Template::Context->add_tag(
            CsvLoopValue=>\&CsvLoopValue  );
sub CsvLoopValue{
    my $ctx = shift;
    $ctx->stash('csv_value');
}
</pre>

<span style='color:magenta'>
package MT::Plugin::CsvLoop;
</span><br/>
関数などの名前が衝突しないように package を指定。
<br/>
<br/>

<span style='color:magenta'>
MT::Template::Context->add_container_tag(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;         CsvLoop=>\&CsvLoop);
</span><br/>
MTCsvLoop というコンテナタグがあると CsvLoop という
メソッドを呼びだすことを指定。
<br/><br/>

<span style='color:magenta'>
my($ctx, $arg)=@_;
</span><br/>
呼び出されるメソッドには２つの引数が渡される。一つ目はMT::Template::Contextオブジェクト。
２つ目はパラメータのハッシュへのリファランス。
<br/><br/>

<span style='color:magenta'>
my(@list)=split(/,/, $arg->{list});
</span><br/>
テンプレートのコンテナタグ内で list='～' と記述した場合、
$arg->{list} に ～ が入る。<br/>
これを "," で区切ったものを @list に代入。
<br/><br/>

あとは、MT::Template::Contextオブジェクトの内容をまだ理解していないので説明できない。悪しからず。(^^;)
<br/><br/>

ここで作ったプラグインを CsvLoop.pl という名で保存し、plugins ディレクトリにアップロードしておこう。
これでテンプレート内で 
コンテナタグの利用は以下のような感じでやれば良い。
<pre>
&lt;MTCsvLoop list='claudius,nero,galba,otho'>
   &lt;$MTCsvLoopValue$>:
   &lt;a href="<$MTCsvLoopValue$>.html">
     &lt;$MTCsvLoopValue$>.html
   &lt;/a>
   &lt;br/>
&lt;/MTCsvLoop>
</pre>
テンプレートの中に固定のループを書いてもあまり役に立たないような気もするが、まあ、サンプルということで。(^^;]]>

</content>
</entry>
<entry>
<title>Movable Type プラグイン作成入門（４）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/movable_type_5.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-07T12:21:59Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.79</id>
<created>2005-03-07T12:21:59Z</created>
<summary type="text/plain">変数タグにパラメータを渡すのも易しい。 例） --- 与えられた文字列を繰り返す...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>MovableType</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[変数タグにパラメータを渡すのも易しい。
<br/><br/>

例）<$MTRepeatStr$> --- 与えられた文字列を繰り返す。
<pre>
package MT::Plugin::RepeatStr;
use strict;
use MT::Template::Context;

MT::Template::Context->add_tag(RepeatStr=> \&repeat);

sub repeat {
	my($ctx, $arg)=@_;
	my $str = $arg->{str} x $arg->{kaisuu};
	$str;
}
1;
</pre>
<br/>


<span style='color:magenta'>
package MT::Plugin::RepeatStr;
</span><br/>
関数などの名前が衝突しないように package を指定。
<br/>
<br/>

<span style='color:magenta'>
MT::Template::Context->add_tag(RepeatStr=>\&repeat);
</span><br/>
RepeatStr というタグがあると repeat という
メソッドを呼びだすことを指定。
<br/><br/>


<span style='color:magenta'>
my($ctx, $arg)=@_;
</span><br/>
呼び出されるメソッドには２つの引数が渡される。一つ目はMT::Template::Contextオブジェクト。
２つ目はパラメータのハッシュへのリファランス。
<br/><br/>

<span style='color:magenta'>
my $str = $arg->{str} x $arg->{kaisuu};
</span><br/>
テンプレートの変数タグ内で str='xyz' kaisuu='99' と記述した場合、
$arg->{str} に xxx, $arg->{kaisuu} に 99 が入る。<br/>
"xyz" x 99  で xyz を 99 回繰り返した文字列が得られる。
<br/><br/>

<span style='color:magenta'>
$str;
</span><br/>
呼び出されるメソッドは最後に結果の文字列を返せば良い。
<br/><br/>


ここで作ったプラグインを RepeatStr.pl という名で保存し、plugins ディレクトリにアップロードしておこう。
これでテンプレート内で 
<span style='color:magenta'>
&lt;$RepeatStr str='-' kaisuu='50' $>
</span>
と記述すれば - を 50 回繰り返した文字列が得られる。
<br/>
<br/>
例）<$MTPerlEval$> --- perl のコードを実行する。
<pre>
package MT::Plugin::Eval;
use strict;
use MT::Template::Context;

MT::Template::Context->add_tag(PerlEval=>\&perleval);

sub repeat {
	my($ctx, $arg)=@_;
	eval($arg->{eval});
}
1;
</pre>
テンプレート内で &lt;$MTPerlEval eval='12*12' $> と記述すると 144 が得られる。便利なようだが、どんな perl のコードでも実行してしまうので危険極まりないとも言える。(^^;
<br/><br/>]]>

</content>
</entry>
<entry>
<title>Movable Type プラグイン作成入門（３）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/movable_type_4.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-07T11:50:48Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.78</id>
<created>2005-03-07T11:50:48Z</created>
<summary type="text/plain">独自の変数タグを作成してテンプレートで使うことも可能だ。 例） --- Yaho...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>MovableType</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[独自の変数タグを作成してテンプレートで使うことも可能だ。
<br/><br/>

例）<$MTYahooLink$> --- Yahoo へのリンクを表示する
<pre>
package MT::Plugin::YahooLink;
use strict;
use MT::Template::Context;

MT::Template::Context->add_tag(YahooLink=>\&yhlink);

sub yhlink {
	my $url="http://www.yahoo.co.jp/";
	"&lt;a href='$url'>$url&lt;/a>";
}
1;
</pre>
<br/>


<span style='color:magenta'>
package MT::Plugin::YahooLink;
</span><br/>
関数などの名前が衝突しないように package を指定。
<br/>
<br/>

<span style='color:magenta'>
MT::Template::Context->add_tag(YahooLink=>\&yhlink);
</span><br/>
MTYahooLink というタグがあると yhlink という
メソッドを呼びだすことを指定。
<br/><br/>


<span style='color:magenta'>
"&lt;a href='$url'>$url&lt;/a>";
</span><br/>
呼び出されるメソッドは最後に結果の文字列を返せば良い。
とてもシンプル。
<br/><br/>

ここで作ったプラグインを yahoolink.pl という名で保存し、plugins ディレクトリにアップロードしておこう。
これでテンプレート内で 
<span style='color:magenta'>
&lt;$MTYahooLink$>
</span>
という変数タグが使えるようになる。
<br/>
<br/>]]>

</content>
</entry>
<entry>
<title>Movable Type プラグイン作成入門（２）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/movable_type_3.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-06T08:48:23Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.77</id>
<created>2005-03-06T08:48:23Z</created>
<summary type="text/plain">グローバルフィルターに引数を渡すことによってもっと柔軟な処理を行うことが可能だ。...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>MovableType</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[グローバルフィルターに引数を渡すことによってもっと柔軟な処理を行うことが可能だ。
<br/><br/>

例） ColorEzo プラグイン --- "tech-ezo" という文字列に任意の色をつける
<pre>
package MT::Plugin::ColorEzo;
use strict;
use MT::Template::Context;
MT::Template::Context->add_global_filter(
            ColorEzo=>\&ClrEzo  );
sub ClrEzo {
    my($text, $arg, $ctx) = @_;
    $text=~s/(tech-ezo)/&lt;font color='$arg'>$1&lt;\/font>/ig;
    $text;
}
1;
</pre>
<br/>

前回の例とほとんど同じである。<br/><br/>

<span style='color:magenta'>
package MT::Plugin::ColorEzo;
</span><br/>
関数などの名前が衝突しないように package を指定。
<br/>
<br/>

<span style='color:magenta'>
MT::Template::Context->add_global_filter(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  ColorEzo=>\&ClrEzo  );
</span><br/>
ColorEzo というフィルタが ClrEzo というメソッドを呼びだすことを指定。
<br/>
<br/>

<span style='color:magenta'>
my ($text, $arg, $ctx) = @_;
</span><br/>
フィルターに渡される３つの引数。一つ目はフィルターをかける文字列。２つ目はテンプレートから渡される引数。３つ目は MT::Template::Contextオブジェクト。<br/>
つまり、色の指定に２つ目の引数を使えば良いのだ。
<br/>
<br/>

<span style='color:magenta'>
    $text =~ s/(tech-ezo)/&lt;font color='$arg'>$1&lt;\/font>/ig;
</span><br/>
"tech-ezo" という文字列を font タグではさむ処理。色の指定に上で渡された２つ目の引数を利用している。
<br/>
<br/>

<span style='color:magenta'>
$text;
</span><br/>
フィルターによって呼び出されるメソッドは、最後に結果の文字列を返す。
<br/><br/>

ここで作ったプラグインを ColorEzo.pl という名で保存し、plugins ディレクトリにアップロードしておこう。
<br/>
<br/>
ColorEzoフィルターを利用するには変数タグやコンテナタグのアトリビュートとして指定すればよい。
<br/>
<br/>
<span style='color:magenta'>
&lt;$MTEntryTitle ColorEzo='blue'$>
</span><br/>
記事のタイトルに対して ColorEzo フィルターを適用。
ここで指定した blue がフィルターのメソッドの２つ目の引数として渡される。
<br/><br/>

<span style='color:magenta'>
&lt;MTEntries lastn="10" ColorEzo='#AA8800'><br/>
... <br/>
&lt;/MTEntries><br/>
</span>
MTEntries ループの中で ColorEzo フィルター適用。
ここで指定した #AA8800 がフィルターのメソッドの２つ目の引数として渡される。
<br/><br/>]]>

</content>
</entry>
<entry>
<title>Movable Type プラグイン作成入門（１）</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2005/03/movable_type_2.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2005-03-06T08:19:53Z</issued>
<id>tag:www.augustus.to,2005:/blog/3128//1.76</id>
<created>2005-03-06T08:19:53Z</created>
<summary type="text/plain">100</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>MovableType</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[Movable Type はプラグインを利用することで新たな機能を柔軟に追加することができる。
<br/>
プラグインにはグローバルフィルター、変数タグ、コンテナタグ、条件タグがある。
<br/>
<br/>
まずはとても小さなグローバルフィルターからはじめてみよう。
<br/>

例） RedEzo プラグイン ～ "tech-ezo" という文字列を赤くする
<pre>
package MT::Plugin::ColorEzo;
use strict;
use MT::Template::Context;

# Global Filter RedEzo の定義
MT::Template::Context->add_global_filter(
	RedEzo => \&ChangeRedEzo);

# Filter によって起動されるメソッド
sub ChangeRedEzo {
    my($text, $arg, $ctx) = @_;
    $text=~s/(tech-ezo)/&lt;font color='red'>$1&lt;\/font>/ig;
    $text;
}
1;
</pre>
<br/><br/>

<span style='color:magenta'>
package MT::Plugin::ColorEzo;
</span><br/>
関数などの名前が衝突しないように package を指定。package の名前は任意だが MT::Plugin::～ という形にするのが慣習。
<br/>
<br/>
<span style='color:magenta'>
MT::Template::Context->add_global_filter(RedEzo=>\&ChangeRedEzo);
</span><br/>
RedEzo というフィルタが ChangeRedEzo というメソッドを呼びだすことを指定。
<br/>
<br/>

<span style='color:magenta'>
my ($text, $arg, $ctx) = @_;
</span><br/>
フィルターには３つの引数が渡される。一つ目はフィルターをかける文字列。２つ目はテンプレートから渡される引数。３つ目は MT::Template::Contextオブジェクト。
<br/>
<br/>

<span style='color:magenta'>
$text=~s/(tech-ezo)/&lt;font color='red'>$1&lt;\/font>/ig;
</span><br/>
"tech-ezo" という文字列を font タグではさむ処理。
<br/>
<br/>

<span style='color:magenta'>
$text;
</span><br/>
フィルターによって呼び出されるメソッドは、最後に結果の文字列を返す。
<br/><br/>

ここで作ったプラグインを RedEzo.pl という名で保存し、plugins ディレクトリにアップロードしておこう。
<br/>
<br/>
RedEzoフィルターを利用するには変数タグやコンテナタグのアトリビュートとして指定すればよい。
<br/>
<br/>
<span style='color:magenta'>
&lt;$MTEntryTitle RedEzo='1'$>
</span><br/>
記事のタイトルに対して RedEzo フィルターを適用
<br/><br/>

<span style='color:magenta'>
&lt;MTEntries lastn="10" RedEzo='1'><br/>
... <br/>
&lt;/MTEntries><br/>
</span>
MTEntries ループの中で RedEzo フィルター適用
<br/><br/>]]>

</content>
</entry>
<entry>
<title>二重起動の防止</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/08/post_3.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-08-05T13:19:33Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.75</id>
<created>2004-08-05T13:19:33Z</created>
<summary type="text/plain">あるプログラムが走っているときに、同時に同じプログラムを起動させたくないときがあ...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[あるプログラムが走っているときに、同時に同じプログラムを起動させたくないときがある。Windows 環境であれば mutex オブジェクトを使うことができ、Perl にも Win32::Mutex モジュールが存在する。
<pre>
use Win32::Mutex;
die "二重起動" if(Win32::Mutex->open('hogehoge'));
$mutex=Win32::Mutex->new(1, 'hogehoge');
while(1){
    print ++$n,"\n";
    sleep(5);
};
</pre>
２行目で他に hogehoge という名前の Mutex オブジェクトが作られていないかどうかチェックし、もし他で hogehoge という名の Mutex オブジェクトが作られていれば、先へ進まずに終了する。３行目では hogehoge という Mutex オブジェクトを作成している。<br/>
したがって、このスクリプトを２つ起動しようとしても、２つ目は２行目のチェックで引っかかって終了してくれるはずである。
<br/><br/>
ただし、タイミングによっては完璧ではないようだ。]]>

</content>
</entry>
<entry>
<title>CSV の行を分解</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/08/csv.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-08-03T13:36:27Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.74</id>
<created>2004-08-03T13:36:27Z</created>
<summary type="text/plain">CSVをコンマで分けるのは一見簡単そうであるが、実は奥が深い。Perl では T...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[CSVをコンマで分けるのは一見簡単そうであるが、実は奥が深い。Perl では Text::CSV_XS モジュールを使うのが便利だ。<br/><br/>

たとえば、split 関数を使って「,」で分ければ、次のような単純な例はうまくいく。
<pre>
$line='A,BB,CCC,,D';
@values=split(/,/,$line);
</pre>
しかし、これでは「,」や「"」を含む値を適切に扱うことはできない。値の中に「,」や「"」を含むときは値を「"」で囲み、含まれる「"」は「""」と記述するのだが、単純に「,」で切っては当然うまくいかない。
<br/><br/>

<a href="http://www.din.or.jp/~ohzaki/perl.htm#CSV2Values">Perlメモ(http://www.din.or.jp/~ohzaki/perl.htm#CSV2Values)</a>に詳しくやり方が書いてあった。難しくて解読困難だ。(^^;
<pre>
$tmp=$line='"""北海道,札幌市""",ABC,"XX,XX","abc"';
$tmp=~s/(?:\x0D\x0A|[\x0D\x0A])?$/,/;
@values=map {/^"(.*)"$/?scalar($_=$1,s/""/"/g,$_):$_}
        ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
</pre>
<br/>

Text::CSV_XS を使って以下のようにすることもできる。日本語を扱わないなら {binary=>1} は不要である。
<pre>
use Text::CSV_XS;
$csv=Text::CSV_XS->new({binary=>1}); #create a object
$line='"北海道,札幌市",ABC,"XX,XX","""abc"""';
$status=$csv->parse($line);          #parse a string
@values=$csv->fields();              #get the fields
print join("\n",@values);
</pre>
Text::CSV_XS は Active Perl なら標準で含まれているからこれが便利だろう。]]>

</content>
</entry>
<entry>
<title>perl でリンクを抽出する</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/08/perl_3.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-08-02T23:54:47Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.73</id>
<created>2004-08-02T23:54:47Z</created>
<summary type="text/plain">perl でリンクを抽出するには HTML::LinkExtor モジュールを使...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[perl でリンクを抽出するには HTML::LinkExtor モジュールを使うのが便利だ。<br/>

<span style='color:magenta'>$p = HTML::LinkExtor->new([$callback[, $base]]);</span><br />
まずは LinkExtor オブジェクトを作るわけだが、 callback 関数を指定しておくと、
リンクを見つけるたびに callback 関数が呼び出される。
callback 関数を指定していないときは 
<span style='color:magenta'>$p->links</span> で
含まれるリンクを明示的に読み出すことができる。<br/>
$base を指定しておくと、相対パスで指定されたリンクを
自動的に $base を基準にしたものとみなして絶対パスに直してくれる。
<br/><br/>

文字列からリンクを抽出したいときは
<span style='color:magenta'>$p->parse($strings)</span>
とするが、
ファイルからリンクを抽出するときは
<span style='color:magenta'>$p->parse_file($filename)</span>
とする。
<br/><br/>

では、http://www.augustus.to/ に含まれるリンクを抽出してみよう。
<pre>
use LWP;
use HTML::LinkExtor;

$url="http://www.augustus.to/";
$browser = LWP::UserAgent->new;
$response = $browser->get($url);

$p = HTML::LinkExtor->new(\&callback,$url);
$p->parse($response->{_content});

sub callback {
    my($tag, %links) = @_;
    print "$tag @{[%links]}\n";
}
</pre>
<br/>


callback 関数を使わないならこんな感じ。
<pre>
use LWP;
use HTML::LinkExtor;

$url="http://www.augustus.to/";
$browser = LWP::UserAgent->new;
$response = $browser->get($url);

$p = HTML::LinkExtor->new(unlink(),$url);
$p->parse($response->{_content});
for $x ($p->links){
    print join(" ", @{$x}),"\n";
}
</pre>
<br/>]]>

</content>
</entry>
<entry>
<title>perl で cookie を取得</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/08/perl_cookie.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-08-02T12:24:43Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.72</id>
<created>2004-08-02T12:24:43Z</created>
<summary type="text/plain">perl で web ページにアクセスするとき、クッキーを取得したいときもあるだ...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[perl で web ページにアクセスするとき、クッキーを取得したいときもあるだろう。そういうときは HTTP::Cookies モジュールを使えばよい。<br/>
以下の例のようにするとページのソースが表示されるとともに、クッキーが指定されたファイルに保存される。
<pre>
use LWP;
use HTTP::Cookies;

$url="http://www.amazon.co.jp/"; #アクセスする URL
$file="cookies_amazon.txt";      #クッキーを保存するファイル

$browser = LWP::UserAgent->new;
$browser->cookie_jar({file =>$file, autosave=>1 });
$response = $browser->get($url);
print $response->{_content};
</pre>]]>

</content>
</entry>
<entry>
<title>ユーザ認証を要するページにperlでアクセス</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/08/perl_2.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-08-01T12:07:33Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.71</id>
<created>2004-08-01T12:07:33Z</created>
<summary type="text/plain">BASIC認証またはDIGEST認証を要求する web ページに perl でア...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>perl</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[BASIC認証またはDIGEST認証を要求する web ページに perl でアクセスしたいときには LWP::UserAgent モジュールの credentials メソッドが使える。<br/>
実験用に用意した http://www.augustus.to/test/authtest/ にアクセスしてみよう。領域名は Auth_Test、ユーザ名は authtestuser、パスワードは password である。スクリプトは以下のようになる。<br/>

<pre>
use LWP;
use HTTP::Request::Common;

$domain="www.augustus.to";
$port=80;
$realm="Auth_Test";   #領域名
$user="authtestuser"; #ユーザ名
$passwd="password";   #パスワード
$url="http://www.augustus.to/test/authtest/";

$browser = LWP::UserAgent->new;
$browser->credentials(
    "$domain:$port",$realm,$user,$passwd);
$response = $browser->get($url);
print $response->{_content};
</pre>]]>

</content>
</entry>
<entry>
<title>waitfor コマンド</title>
<link rel="alternate" type="text/html" href="http://www.augustus.to/blog/3128/archives/2004/05/waitfor.html" />
<modified>2005-11-11T17:04:58Z</modified>
<issued>2004-05-16T10:33:37Z</issued>
<id>tag:www.augustus.to,2004:/blog/3128//1.70</id>
<created>2004-05-16T10:33:37Z</created>
<summary type="text/plain">Waitfor コマンドは Windows Server 2003 からの新しい...</summary>
<author>
<name>augustus</name>
<url>http://www.augustus.to/</url>
<email>augustus06@hear.to</email>
</author>
<dc:subject>システム管理</dc:subject>
<content type="text/html" mode="escaped" xml:lang="ja" xml:base="http://www.augustus.to/blog/3128/">
<![CDATA[<p>Waitfor コマンドは Windows Server 2003 からの新しいコマンドで、シグナルを用いて複数のコンピュータの同期をとることができる。</p>

<p>構文１：シグナルを待つ<br />
<span style='color:magenta'>waitfor [/t TimeoutInSeconds] SignalName</span><br />
SignalName という名のシグナルを待つ。シグナルが来るか、TimeoutInSeconds 秒経過するとこのコマンドは終了する。「/t TimeoutInSeconds」を省略したときは無期限にシグナルを待機し続ける。</p>

<p>構文２：シグナルを送る<br />
<span style='color:magenta'>waitfor [/s Computer [/u [Domain\]User [/p [Password]]]] /si SignalName</span><br />
/s  シグナルの送り先コンピュータ名またはIPを指定。規定値はローカルコンピュータ。<br />
/si 送るシグナルの名前を指定。<br />
/u  指定したユーザの権限で実行。 /p でパスワードも指定する。</p>

<p>これを使うと複数のバッチを同期させたり、複数のコンピュータの同期をバッチで行うことが可能である。<br />
以前は同様のことをしたいときは、テンポラリのファイルを作成するなどして実現していたと聞く。 waitfor コマンドを使うと、ゴミも残らず、タイムアウトも指定できるなど利点が大きい。</p>

<p><br />
例：<br />
comp1 で foo1.bat, comp2 で foo2.bat を実行する。 foo2.bat の終了を foo1.bat の中で待つ。</p>

<p>##### comp1 / foo1.bat #####<br />
echo comp2 の foo2.bat を待ちます。<br />
waitfor foosignal<br />
echo comp2 の foo2.bat は終わったようだ。</p>

<p>##### comp2 / foo2.bat #####<br />
echo foo2.bat がスタートします。<br />
////// 時間のかかる仕事 //////<br />
waitfor /s comp1 /si foosignal</p>]]>

</content>
</entry>

</feed>