example.pl untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 110 CC 8 SCAR 27.2
CC
8
Cov
0%
color
30.0
BEGIN
20.0
joina
2.0
CRAP 15.2
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5 use v5.16;
6  
7 use Template::Sluz;
8  
9 ###############################################################################
10 ###############################################################################
11  
12 my $s = Template::Sluz->new();
13  
14 $s->assign('name' , "Jason");
15 $s->assign('colors' , ['red','green', 'blue']);
16 $s->assign('data' , { color => 'red', animal => 'kitten', age => 39 });
17 $s->assign('version', $Template::Sluz::VERSION);
18 $s->assign('color_white', color('bold_white'));
19 $s->assign('color_yellow', color(228));
20 $s->assign('color_reset', color('reset'));
21  
22 print $s->fetch(SLUZ_INLINE);
23  
24 print $s->parse_string('HTML: <div>Name: {$name} / Colors: {$colors|join:", "}</div>') . "\n";
25  
26 $s->set_delimiters("<", ">");
27 print $s->parse_string('JSON: {"name": "<$name>", "animal": "<$data.animal>"}') . "\n";
28  
29 ###############################################################################
30 ###############################################################################
31  
32 sub joina {
33 my $arr = shift();
34 my $glue = shift();
35  
36 my $ret = join($glue, @$arr);
37  
38 return $ret;
39 }
40  
41 # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
42 sub color {
43 my ($str, $txt) = @_;
44  
45 state $notty = !-t STDOUT; # Cache the TTY check
46 if ($notty || $ENV{NO_COLOR}) { return $txt // ""; } # No interactive terminal
47 if (!length($str) || $str eq 'reset') { return "\e[0m"; } # No string = RESET
48  
49 # Some predefined colors/commands
50 state %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
51 state %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
52  
53 # Pre-process the string
54 $str =~ s/on_/-/g; # "on_" becomes a negative number
55 $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg; # color name -> number
56  
57 my @parts = map { # If it's negative it's a background color, otherwise foreground
58 $cmd_map{$_} // ($_ =~ /^-(.+)/ ? "48;5;$1" : "38;5;$_")
59 } split("_", $str);
60  
61 my $ret = "\e[" . join(";", @parts) . "m";
62  
63 if (defined $txt) { $ret .= $txt . "\e[0m"; }
64  
65 return $ret;
66 }
67  
68 # Creates methods k() and kd() to print, and print & die respectively
69 BEGIN {
70 if (!defined(&trim)) {
71 *trim = sub {
72 my ($s) = (@_, $_); # Passed in var, or default to $_
73 if (length($s) == 0) { return ""; }
74 $s =~ s/^\s*//;
75 $s =~ s/\s*$//;
76  
77 return $s;
78 }
79 }
80  
81 if (eval { require Dump::Krumo }) {
82 Dump::Krumo->import(qw/k kd/);
83 } else {
84 require Data::Dumper;
85 *k = sub { print Data::Dumper::Dumper(\@_) };
86 *kd = sub { print Data::Dumper::Dumper(\@_); die; };
87 }
88 }
89  
90 __DATA__