| 1 | #!/usr/bin/env perl | ||
| 2 | 1 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Text::Stencil; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # chaining transforms: trim, then truncate, then html-escape | ||
| 7 | 1 | my $chain = Text::Stencil->new(row => '{0:trim|trunc:30|html}', separator => "\n"); | |
| 8 | 1 | say "=== chained ==="; | |
| 9 | 1 | say $chain->render([ | |
| 10 | [' <script>alert("long text that will be truncated")</script> '], | ||
| 11 | [' Short & safe '], | ||
| 12 | ]); | ||
| 13 | |||
| 14 | # conditional output | ||
| 15 | 1 | my $cond = Text::Stencil->new(row => '{name:raw}{active:if: (active)}{role:wrap: [:]}', separator => "\n"); | |
| 16 | 1 | say "\n=== conditional ==="; | |
| 17 | 1 | say $cond->render([ | |
| 18 | { name => 'Alice', active => 1, role => 'admin' }, | ||
| 19 | { name => 'Bob', active => 0, role => '' }, | ||
| 20 | { name => 'Eve', active => 1, role => 'user' }, | ||
| 21 | ]); | ||
| 22 | |||
| 23 | # map transform | ||
| 24 | 1 | my $status = Text::Stencil->new(row => '{name:raw}: {code:map:0=OK:1=WARN:2=ERROR:*=UNKNOWN}', separator => "\n"); | |
| 25 | 1 | say "\n=== map ==="; | |
| 26 | 1 | say $status->render([ | |
| 27 | { name => 'server1', code => '0' }, | ||
| 28 | { name => 'server2', code => '2' }, | ||
| 29 | { name => 'server3', code => '9' }, | ||
| 30 | ]); | ||
| 31 | |||
| 32 | # coalesce: fallback fields | ||
| 33 | 1 | my $coal = Text::Stencil->new(row => '{display:coalesce:nickname:name:Anonymous}', separator => "\n"); | |
| 34 | 1 | say "\n=== coalesce ==="; | |
| 35 | 1 | say $coal->render([ | |
| 36 | { name => 'Alice', nickname => 'Al', display => 'Alice A.' }, | ||
| 37 | { name => 'Bob', nickname => '', display => '' }, | ||
| 38 | { name => '', nickname => '', display => '' }, | ||
| 39 | ]); | ||
| 40 | |||
| 41 | # formatting: numbers, dates, masks | ||
| 42 | 1 | my $fmt = Text::Stencil->new( | |
| 43 | row => '{0:int_comma} | {1:bytes_si} | {2:elapsed} | {3:mask:4}', | ||
| 44 | separator => "\n", | ||
| 45 | ); | ||
| 46 | 1 | say "\n=== formatting ==="; | |
| 47 | 1 | say $fmt->render([ | |
| 48 | [1234567, 1073741824, 90061, '4111222233334444'], | ||
| 49 | [42, 1536, 45, 'secret'], | ||
| 50 | ]); | ||