blib/lib/PYX/Optimization.pm

filter: Statementstmt 100% 67 / 67 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 100% 89 / 89 CC 3 SCAR 2.2
CC
3
Cov
100%
_data
2.0
_comment
2.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 package PYX::Optimization;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use PYX qw(char comment);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use PYX::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use PYX::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = 0.06;
14  
15 # Constructor.
16 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 6 my ($class, @params) = @_;
18 6 my $self = bless {}, $class;
19  
20 # Output encoding.
21 6 $self->{'output_encoding'} = 'utf-8';
22  
23 # Output handler.
24 6 $self->{'output_handler'} = \*STDOUT;
25  
26 # Process params.
27 6 set_params($self, @params);
28  
29 # PYX::Parser object.
30 $self->{'pyx_parser'} = PYX::Parser->new(
31 'output_encoding' => $self->{'output_encoding'},
32 'output_handler' => $self->{'output_handler'},
33 'output_rewrite' => 1,
34 'callbacks' => {
35 'data' => \&_data,
36 'comment' => \&_comment,
37 },
38 'non_parser_options' => {
39 4 'output_encoding' => $self->{'output_encoding'},
40 },
41 );
42  
43 # Object.
44 4 return $self;
45 }
46  
47 # Parse pyx text or array of pyx text.
48 6 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
49 6 my ($self, $pyx, $out) = @_;
50  
51 6 $self->{'pyx_parser'}->parse($pyx, $out);
52  
53 6 return;
54 }
55  
56 # Parse file with pyx text.
57 6 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
58 6 my ($self, $file, $out) = @_;
59  
60 6 $self->{'pyx_parser'}->parse_file($file, $out);
61  
62 6 return;
63 }
64  
65 # Parse from handler.
66 6 ▶sub parse_handler {
Subroutine parse_handler: called CC=1 SCAR=0.0
Pod documented
67 6 my ($self, $input_file_handler, $out) = @_;
68  
69 6 $self->{'pyx_parser'}->parse_handler($input_file_handler, $out);
70  
71 6 return;
72 }
73  
74 # Process data.
75 63 ▶sub _data {
Subroutine _data: called CC=2 SCAR=6.9
76 63 my ($pyx_parser_obj, $data) = @_;
77  
78 63 my $tmp = PYX::Utils::encode($data);
79 63 ▶ if ($tmp =~ /^[\s\n]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /^[\s\n]*$/) 6 57 63
80 6 return;
81 }
82  
83 # TODO Preserve?
84  
85 # White space on begin of data.
86 57 $tmp =~ s/^[\s\n]*//s;
87  
88 # White space on end of data.
89 57 $tmp =~ s/[\s\n]*$//s;
90  
91 # White space on middle of data.
92 57 $tmp =~ s/[\s\n]+/\ /sg;
93  
94 57 $data = PYX::Utils::decode($tmp);
95 57 my $out = $pyx_parser_obj->{'output_handler'};
96 my $encoded_output = Encode::encode(
97 57 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
98 char($data),
99 );
100 57 print {$out} $encoded_output, "\n";
101  
102 57 return;
103 }
104  
105 # Process comment.
106 24 ▶sub _comment {
Subroutine _comment: called CC=2 SCAR=6.9
107 24 my ($pyx_parser_obj, $comment) = @_;
108  
109 24 my $tmp = PYX::Utils::encode($comment);
110 24 ▶ if ($tmp =~ /^[\s\n]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /^[\s\n]*$/) 3 21 24
111 3 return;
112 }
113 21 $tmp =~ s/^[\s\n]*//s;
114 21 $tmp =~ s/[\s\n]*$//s;
115 21 $comment = PYX::Utils::decode($tmp);
116 21 my $out = $pyx_parser_obj->{'output_handler'};
117 my $encoded_output = Encode::encode(
118 21 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
119 comment($comment),
120 );
121 21 print {$out} $encoded_output, "\n";
122  
123 21 return;
124 }
125  
126 6 1;
127  
128 __END__
↑ Summary