ClearSilver ã® Perl ãã¤ã³ãã£ã³ã°ã使ã(Perlã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®ãã³ããã¼ã¯)
ClearSilverãã¿ãç¶ãã¦ãããã ãã©ãä»åº¦ã¯Perlãã¤ã³ãã£ã³ã°ã使ã£ã¦ã¿ãã
ClearSilverãã¤ã³ã¹ãã¼ã«ããã¨åæã«Perlãã¤ã³ãã£ã³ã°ãã¤ã³ã¹ãã¼ã«ããããã ãã©ãPODã«ã¯ä½ã«ãæ¸ãã¦ãªãã»ã»
ここã«APIãã®ã£ã¦ãã ãã
ã¨ããããã§ãµã³ãã«ã¯ä»¥ä¸ã
#!/usr/local/bin/perl use strict; use warnings; use ClearSilver; my $hdf = ClearSilver::HDF->new; $hdf->setValue('message', 'hello world!'); my $cs = ClearSilver::CS->new($hdf); $cs->parseFile('hello.cs'); print $cs->render;
æ°ã«ãªã£ãç¹ã¯
- TTã«ãªãã¦ãã®ã§ãã¡ã½ããå¼ã³åºããã§ããªãã®ãä¸çªä¸ä¾¿(å½ããåã ãã©)
- HashãListããã®ã¾ã¾æ¸¡ããªãã®ãä¸ä¾¿
my $hash = { Foo => 'Foo', Bar => 'Bar' }; $hdf->setValue('Hash', $hash);
ã£ã¦ããã¨
Hash { Foo = Foo Bar = Bar }
ã£ã¦ãªã£ã¦ã»ããã
çµå±ãããªæãã®ã©ããã¼ã使ããããªãããªã
sub hdf_setValue { my ($hdf, $key, $val) = @_; if (ref $val eq 'ARRAY') { my $index = 0; for my $v (@$val) { hdf_setValue($hdf, "$key.$index", $v); $index++; } } elsif (ref $val eq 'HASH') { while (my ($k, $v) = each %$val) { hdf_setValue($hdf, "$key.$k", $v); } } elsif (ref $val eq 'SCALAR') { hdf_setValue($hdf, $key, $$val); } elsif (ref $val eq '') { $hdf->setValue($key, $val); } }
ã£ã¦ããããã§ãPerlã§ä½¿ãã®ã¯çµæ§ä¸ä¾¿ã
ãã ãããã¡ããã¡ãéãã
Template-ToolkitãClearSilverãHTML::Templateã§ãã³ãåã£ã¦ã¿ãã
benchmark.pl
#!/usr/local/bin/perl use strict; use warnings; use Benchmark qw(timethese cmpthese); use Template; use ClearSilver; use HTML::Template; my $bench = timethese(10000, { 'Template-Toolkit' => sub { my $tt = Template->new; my $out; $tt->process('hello.tmpl', { message => 'hello world!' }, \$out); }, 'ClearSilver' => sub { my $hdf = ClearSilver::HDF->new; $hdf->setValue('message', 'hello world!'); my $cs = ClearSilver::CS->new($hdf); $cs->parseFile('hello.tmpl'); my $out = $cs->render; }, 'HTML::Template' => sub { my $template = HTML::Template->new(filename => 'hello.tmpl'); $template->param(message => 'hello world!'); my $out = $template->output; }, }, ); cmpthese($bench);
hello.tmpl
[% message %] <?cs var:message ?> <TMPL_VAR NAME=message>
ããããããªãã
Rate | Template-Toolkit | HTML::Template | ClearSilver | |
---|---|---|---|---|
Template-Toolkit | 592/s | -- | -82% | -97% |
HTML::Template | 3247/s | 448% | -- | -86% |
ClearSilver | 23256/s | 3828% | 616% | -- |
ãªãã ããã»ã»ã»éããã
è¤éãªãã³ãã¬ã¼ãã¯è©¦ãã¦ãªããã©ã
ã¨ã«ããClearSilverã«ã¯æ³¨ç®ã