0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Template-Toolkit

Last updated at Posted at 2015-12-01

断捨離

インストール

$ cpanm Template

環境によっては、 sudo

データ構造の出力に関して。

hash

$ pbpaste
use Template ;
my %hash = map{ $_ , 1 } qw( 1 2 3) ;
### 1. for my $x ( sort keys %hash ){ }
Template->new->process(\<<'EOF', { rel => \%hash } ) ;
[% FOR x IN rel.keys -%]
    [%- x _ "\t" _ rel.${x} %]
[% END -%]
EOF

$ pbpaste | perl
1       1
2       1
3       1

each

$ pbpaste
use Template ;
my %hash = map{ $_ , 1 } qw( 1 2 3) ;
Template->new->process(\<<'EOF', { rel => \%hash } ) ;
[% FOR x IN rel -%]
    [%- x.key _ "\t" _ x.value %]
[% END -%]
EOF

結果は上に同じ。

ソート

$ pbpaste
use Template ;
my %hash = map{ $_ , 1 } qw( 10 0002 1) ;
Template->new->process(\<<'EOF', { hash_ref => \%hash } ) ;
[%- cnt = 0 -%]
[% FOR x = hash_ref.keys.sort -%]
    [%- x _ "\t" %][% hash_ref.${x} _ "\t" %][% cnt %]
[% cnt = cnt + 1 ; END -%]
EOF

$ pbpaste | perl
0002	1	0
1	1	1
10	1	2

文字列ソート。

pairs

$ pbpaste
use Template ;
my %hash = map{ $_ , 1 } qw( 3 2 1 ) ;
Template->new->process(\<<'EOF', { hash_ref => \%hash } ) ;
[% FOR x = hash_ref.pairs -%]
    [%- x.key _ "\t" _ x.value %]
[% END %]
EOF

$ pbpaste | perl
1	1
2	1
3	1

勝手にソート

複数配列

$ pbpaste
use Template ;
my @arr2 = qw( foo bar baz ) ;
my @arr3 = qw( 1 2 3 ) ;
Template->new->process(\<<'EOF', { arr2_ref => \@arr2, arr3_ref => \@arr3 } ) ;
[% FOR x = [ 0 .. arr2_ref.max ] -%]
    [%- arr2_ref.$x %] [% arr3_ref.$x %]
[% END -%]
EOF

$ pbpaste | perl
foo 1
bar 2
baz 3

2次元配列の扱い。

$ pbpaste
use Template ;
my @arr = (
        [ 1, 2, 3 ],
        [ 4, 5, 6 ],
        [ 7, 8, 9 ],
    ) ;

Template->new->process(\<<'EOF', { rel => \@arr } ) ;
[%- FOR x = rel -%]
    [%- FOR y = x -%]
        [%- y -%]
    [%- END -%]
[%- END %]
EOF

$ pbpaste | perl
123456789

要素へのアクセス

@arr 宣言までは上に同じ

Template->new->process(\<<'EOF', { rel => \@arr } ) ;
[%- FOR x = rel -%]
    [%- x.0 _ ":" _ x.1 _ ":" _ x.2  %]
[% END %]
EOF

$ pbpaste | perl
1:2:3
4:5:6
7:8:9

slice と join

@arr 宣言までは上に同じ

Template->new->process(\<<'EOF', { rel => \@arr } ) ;
[%- FOR x = rel -%]
    [%- first = x.slice(0,1) -%]
    [%- first.join("\t") _ ":" _ x.2 %]
[% END -%]
EOF

$ pbpaste | perl
1	2:3
4	5:6
7	8:9

AoH

$ pbpaste
use Template ;
my @arr = (
    { count => 2, name  => 'joe'},
    { count => 1, name  => 'mike'},
    { count => 2, name  => 'lara'},
) ;

Template->new->process(\<<'EOF', { rel => \@arr } ) ;
[%- FOR x IN rel -%]
    [%- x.name _ "\t" _ x.count %]
[% END -%]
EOF

$ pbpaste | perl
joe	2
mike	1
lara	2

fasta 作製。

共通部分

use Template ;
my $org_seq = 'a' x 1000 ;
my $name = q{test} ;
my $seq ;
my $ref = {
    name => $name,
    data => $org_seq,
} ;
Template->new->process(\<<'EOF', $ref, \$seq ) ;

ヒアドキュメントを使ってるので直後から続きを。

match, for 文使用

>[% name %]
[% arr = data.match('.{1,50}', 1) -%]
[%- FOR i = arr -%]
    [%- i %]
[% END -%]
EOF

print $seq ;

match

>[% name %]
[% data.match('.{1,50}', 1).join("\n") %]
EOF

print $seq ;

split => replace

>[% name %]
[% data.replace('(.{1,50})', '$1 ').split(' ').join("\n") %]
EOF

print $seq ;

chunk

>[% name %]
[% data.chunk(42).join("\n") %]
EOF

print $seq ;

雑多

DATA トークン利用

\ <<'EOF'\* DATA にする事で、
DATA トークンを利用する。

use Template ;
my $tt = Template->new({ EVAL_PERL    => 1}) ;
$tt->process(\*DATA, ) ;

# something code ;

__DATA__
DATA token

chunk の実用

$ pbpaste
use Template ;
Template->new->process(
    \'[% data.chunk(-3).join(",") _ "\n" %]',
    { data => 10000000 }
) ;

$ pbpaste | perl
10,000,000

strftime

$ pbpaste
use Template ;
Template->new->process(\<<'EOF',) ;
[% USE date(format = '%Y-%m-%d %H:%M:%S') -%]
[%- date.format %]
EOF

$ pbpaste | perl
2010-10-26 16:56:31

EVAL PERL

use Template ;
my $tt = Template->new({ EVAL_PERL    => 1}) ;
$tt->process(\<<'EOF', ) ;
[%- PERL -%]
open my $fh , 'something.txt' or die ;
my @line = <$fh> ;
close $fh ;
print @line  ;
[%- END -%]
EOF

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?