Hokkaido.pm Casual#5に行ってきました
今回も、無事にHokkaido.pm Casual#5を行うことが出来ました。
次回は、10/17を予定しておりますので、よろしくお願いします。
まずは、みなさんお疲れさまでした!
次回は、O/Rマッパーでいんですかね?DBIだっけ??
あんまり良く覚えてないデス・・・、ゴメンなさい!
「Perl Beginners」からいろいろパクりまして(*1)、オープニングトークをして、
その中で、LTで質問するっていう手もあるよ!っていうお話をしました。
あと、「Perl入学式」のように補講はできないにしても、
過去に扱ったテーマ(発表)に対するリクエストを募集している旨を伝えました。
という訳で、さっそく内容の方に。
まとめるとこんな感じ。
- テンプレートって?
- みんなどれ使ってるの?
- カジュアルに使うならどれ?
で、テンプレート使うとこんなことできるよ!みたいなお話は後にして、
どれを使ってるの?っていうお話だと、まずは候補がこんな感じ。
Template-Toolkit
HTML::Template
HTML::Template::Pro
Text::Xslate
Text::MicroTemplete
で、カジュアルなのはformat関数でしょ!
じゃなくて、Text::MicroTempleteが依存が少ないし、
レンタルサーバーでも使えるって意味でカジュアルですね、っていう結論(*2)でした。
もちろん、環境が許すならText::Xslateだと思います。
次に、PerlのI/Oネタ。
こんな感じで、日々の疑問をLTで提供して貰えるとうれしいです。
perldocって、ほんと便利ですね。
use strict;
use warnings;
use JSON;
use utf8;
use Encode;
my $utf8_text = '[{"名前":"あ"},{"名前":"い"}]';
# perldoc JSON より
# $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref;
# $perl_hash_or_arrayref = decode_json $utf8_encoded_json_text;
my $array_ref = decode_json( encode_utf8($utf8_text) );
foreach my $hash_ref (@{$array_ref}) {
print encode_utf8("名前 => " . $hash_ref->{"名前"}), "\n";
$hash_ref->{"年齢"} = "123";
}
print encode_json($array_ref), "\n";
# 出力結果
# 名前 => あ
# 名前 => い
# [{"年齢":"123","名前":"あ"},{"年齢":"123","名前":"い"}]
最後に、自分が発表したネタ。

この画像をCairoで作るのにこんなコードを書きました。
use strict;
use warnings;
use Cairo;
use Math::Trig qw( deg2rad );
use Data::Dumper;
my ( $w, $h ) = ( 121, 121 );
my $surface = Cairo::ImageSurface->create( 'argb32', $w, $h );
my $cr = Cairo::Context->create( $surface );
$cr->set_line_width( 2 );
#$cr->set_antialias( 'none' );
my $n = 16;
my ( $x0, $y0 ) = ( int($w / 2), int($h / 2) );
my $r = 30;
my $da = 360 / $n;
for (my $i=0; $i<$n; $i++) {
$cr->translate( $x0, $y0 );
$cr->rotate( deg2rad($da * $i) );
$cr->translate( -$x0, -$y0 );
$cr->rectangle( $x0 + $r, $y0, 15, 20 );
$cr->set_source_rgb( 0, 0, 0 );
$cr->stroke();
$cr->identity_matrix();
}
$cr->show_page();
$surface->write_to_png( 'output.png' );
MacじゃないとCairoの入れ方とか説明できないし、
無理矢理テンプレートに関連付けると、
SVGをテンプレートで出力する方法があるよ!っていう流れから、
use strict;
use warnings;
use Text::Xslate;
use Data::Section::Simple;
my $vpath = Data::Section::Simple->new()->get_data_section();
my $xslate = Text::Xslate->new(
path => [$vpath],
);
my ( $w, $h ) = ( 121, 121 );
my $n = 16;
my ( $x0, $y0 ) = ( int($w / 2), int($h / 2) );
my $r = 30;
my $da = 360 / $n;
my @rectangles = ();
for (my $i=0; $i<$n; $i++) {
push @rectangles, {
r => $r,
a => $i * $da,
x0 => $x0,
y0 => $y0
};
};
print $xslate->render( 'svg.tx', {
width => $w,
height => $h,
rectangles => \@rectangles
} );
__DATA__
@@ svg.tx
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>SVG</title>
<style type="text/css">
<!--
rect {
fill: none;
stroke: black;
stroke-width: 2;
}
-->
</style>
</head>
<body>
<svg width="<: $width :>" height="<: $height :>"
xmlns="http://www.w3.org/2000/svg" version="1.1">
: for $rectangles -> $rect {
<rect x="<: $rect.x0 + $rect.r :>" y="<: $rect.y0 :>" width="15" height="20"
transform="rotate(<: $rect.a :> <: $rect.x0 :> <: $rect.y0 :>)" />
: }
</svg>
</body>
このスクリプトを”aaa.pl”とかで保存して、
$ perl aaa.pl > index.html
を実行すると、”index.html”が生成されて、Chromeで同じ絵が確認できました。
あと、これを実行するためのモジュールは、
$ cpanm Text::Xslate
$ cpanm Data::Section::Simple
で、インストールすることができます。
配列リファレンスとハッシュリファレンスがポイントですかね。
テンプレート内では、”->”ではなく”.”でメンバーを参照しています。
スライドは間に合わなかったけど、ちゃんと動いたので良かった!
おしまい。
(*1) オープニングトークとLTで質問
(*2) #hokkaido.pm@freenode(IRC)調べだと、printfとheredocが上がってました
Leave a Comment