blib/lib/Template/Plugin/Filter/IDN.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 92.0% 46 / 50 CC 5 SCAR 9.9
CC
5
Cov
95%
filter
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.7
↑ Summary
1 #
2 # This file is part of Template-Plugin-Filter-IDN
3 #
4 # This software is copyright (c) 2018 by Michael Schout.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9  
10 package Template::Plugin::Filter::IDN;
11 2 $Template::Plugin::Filter::IDN::VERSION = '0.03';
12 # ABSTRACT: Template Toolkit plugin for encoding and decoding International Domain Names.
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use syntax 'junction';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use parent 'Template::Plugin::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Net::IDN::Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our $DYNAMIC = 1;
24  
25 sub init {
26 13 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
27  
28 13 $self->install_filter('idn');
29  
30 13 return $self;
31 }
32  
33 sub filter {
34 13 ▶ my ($self, $text, $args) = @_;
Subroutine filter: called CC=5 SCAR=16.2
Pod undocumented
35  
36 13 my ($type) = @$args;
37  
38 # if no "type" was given, try to guess what we should do. If we have
39 # non-ascii chars, assume that we want to_ascii
40 13 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 2 11 13
41 2 ▶ $type = ($text =~ /[^ -~\s]/) ? 'to_ascii' : 'to_utf8';
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[^ -~\s]/ ? : 1 1 2
42 }
43  
44 13 ▶ if ($type eq any(qw(encode to_ascii))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq any(('encode', 'to_ascii'))) { } 6 7 13
elsif ($type eq any(('decode', 'to_utf8'))) { } 7 0 7
45 6 return Net::IDN::Encode::domain_to_ascii($text);
46 }
47 elsif ($type eq any(qw(decode to_utf8))) {
48 7 return Net::IDN::Encode::domain_to_unicode($text);
49 }
50 else {
51 0 Carp::croak "Unknown IDN filter action: $type";
52 }
53 }
54  
55 2 1;
56  
57 __END__
↑ Summary