| 1 | package Templ::Util; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | require Exporter; | |||||||||
| 4 | 1 | our @ISA = qw(Exporter); | |||||||||
| 5 | our @EXPORT_OK | ||||||||||
| 6 | 1 | = qw(attempt quote unquote default indent undent number_lines headerize expand_isa); | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | # Run a block of code, return 0 if the code died, 1 if it didn't | ||||||||||
| 14 | sub attempt (&) { | ||||||||||
| 15 | 2 | ▶ | my $sub = shift; | ||||||||
|
Subroutine
attempt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 16 | 2 | eval { $sub->(); }; | |||||||||
| 17 | 2 | ▶ | return $@ ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | # Escapes a single-quoted string | ||||||||||
| 21 | sub quote ($) { | ||||||||||
| 22 | 4 | ▶ | my $str = shift; | ||||||||
|
Subroutine
quote: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 23 | 4 | $str =~ s|\\|\\\\|gs; | |||||||||
| 24 | 4 | $str =~ s|'|\\'|gs; | |||||||||
| 25 | 4 | return $str; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | # Escapes a single-quoted string | ||||||||||
| 29 | sub unquote ($) { | ||||||||||
| 30 | 18 | ▶ | my $str = shift; | ||||||||
|
Subroutine
unquote: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 31 | 18 | $str =~ s|\\\\|\\|gs; | |||||||||
| 32 | 18 | $str =~ s|\\'|'|gs; | |||||||||
| 33 | 18 | return $str; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | # Returns the first defined value in a list, or a blank string if there are | ||||||||||
| 37 | # no defined values | ||||||||||
| 38 | sub default (@) { | ||||||||||
| 39 | 51 | ▶ | foreach (@_) { defined($_) && return $_; } | ||||||||
|
Subroutine
default: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 40 | 0 | return ''; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | sub undent ($) { | ||||||||||
| 44 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 1 | ▶ | return $_[0] if ( $_[0] !~ m/^(?:\r?\n)*([ \t]+)/ ); | ||||||||
|
Subroutine
undent: called CC=2 SCAR=7.0
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 46 | 1 | my $i = $1; | |||||||||
| 47 | 1 | return join '', map { s/^\Q$i\E//; $_ } grep { $_ ne '' } | |||||||||
| 48 | split /(.*?\n)/, $_[0]; | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub indent ($$) { | ||||||||||
| 52 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 0 | ▶ | return join '', map {"$_[0]$_"} grep { $_ ne '' } split /(.*?\n)/, $_[1]; | ||||||||
|
Subroutine
indent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub headerize ($$) { | ||||||||||
| 57 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 0 | ▶ | return join '', map {"$_[0]$_\n"} split /\n/, $_[1]; | ||||||||
|
Subroutine
headerize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub number_lines ($) { | ||||||||||
| 62 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 0 | ▶ | my @lines = split /\n/, $_[0]; | ||||||||
|
Subroutine
number_lines: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 64 | 0 | my $format = '%' . length( scalar(@lines) . '' ) . "s: %s\n"; | |||||||||
| 65 | 0 | return join '', map { sprintf( $format, ( $_ + 1 ), $lines[$_] ) } | |||||||||
| 66 | 0 .. $#lines; | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # Inspired by Class::ISA::self_and_super_path | ||||||||||
| 70 | sub expand_isa { | ||||||||||
| 71 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 38 | ▶ | my $seen = ref($_[0]) eq 'HASH' ? shift @_ : {}; | ||||||||
|
Subroutine
expand_isa: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 73 | return | ||||||||||
| 74 | 72 | map { s/^::/main::/; $_ } | |||||||||
| 75 | 72 | ▶ | grep { defined($_) && $_ ne '' } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 30 | map { $_, expand_isa( $seen, @{ $_ . '::ISA' } ) } | |||||||||
| 77 | 38 | grep { !$seen->{$_}++ } | |||||||||
| 78 | @_; | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 1 | 1; | |||||||||