Perl 5.9 以降でPDFJ-0.90 を使う

前置き

PDF を作れという指令がきたので,どこぞで耳に挟んだPDFJ を使うなどしようと思ったのだけど,Perl 5.9 以降では使えなくて,意外にWeb に情報が無いようだったので,とりあえず情報を残すことにする.

PDFJ は,内部でpseudo-hash (phash, 仮想ハッシュ)を使っている.
Perl 5.9 以降ではphash が削除されたためエラーとなる.

例えばこんな記事

phash は,以下のようになっている.*1

# phash のイメージ
$phash = [
    hash_ref, # キーと位置を指定する(1-origin)
    array_body1, array_body2, ...,
];

phash の例
$ perl -v | egrep built
This is perl, v5.8.9 built for i686-linux
$ perl -le '$h=[{a=>1,b=>2}, qw(c d)]; print $h->{a}; print $h->[2]'
c
d

例えば,こんな感じでphash から hash に変換できる.

sub phash2hash {
    my ($pos) = @_;
    my @ary = @_;
    my %hash;
    foreach my $k (keys(%$pos)) {
        $hash{$k} = $ary[$pos->{$k}];
    }
    return \%hash;
}

実行結果

$ perl -v|egrep built
This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
$ git branch
*1235881643* master
  perl-5.10-patch
$ perl sample/nouhinsho.pl sample/nouhinsho.dat a.pdf
Not a HASH reference at PDFJ.pm line 4040, <DATA> line 3873.
[1]    14683 exit 255   perl sample/nouhinsho.pl sample/nouhinsho.dat a.pdf
$ git co perl-5.10-patch
Switched to branch "perl-5.10-patch"
$ perl sample/nouhinsho.pl sample/nouhinsho.dat a.pdf
$ ls a.pdf
a.pdf

fswiki は,PDFJ 0.7 を使っているようだから,そのままパッチ当てらんないかも...

$ egrep VERSION wiki3_6_3_1/lib/PDFJ.pm
use vars qw($VERSION @EXFUNC %Default);
$VERSION = 0.7;

うぅ.まだ読んでないなぁ
http://www.amazon.co.jp/exec/obidos/ASIN/4798119172/search042-22/ref=nosim/
モダンPerl入門 (CodeZine BOOKS)

*1:PDFJ は行儀が良くて,phash を配列として使うみたいな変なことはしていなかった