| 1 | package PYX::Hist; | ||||||||||
| 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 Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ▶ | use List::Util qw(reduce); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use PYX::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 6 | our $VERSION = 0.07; | |||||||||
| 12 | |||||||||||
| 13 | # Constructor. | ||||||||||
| 14 | sub new { | ||||||||||
| 15 | 12 | ▶ | my ($class, @params) = @_; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 16 | 12 | my $self = bless {}, $class; | |||||||||
| 17 | |||||||||||
| 18 | # Output handler. | ||||||||||
| 19 | 12 | $self->{'output_handler'} = \*STDOUT; | |||||||||
| 20 | |||||||||||
| 21 | # Process params. | ||||||||||
| 22 | 12 | set_params($self, @params); | |||||||||
| 23 | |||||||||||
| 24 | # PYX::Parser object. | ||||||||||
| 25 | $self->{'_pyx_parser'} = PYX::Parser->new( | ||||||||||
| 26 | 'callbacks' => { | ||||||||||
| 27 | 'end_element' => \&_end_element, | ||||||||||
| 28 | 'final' => \&_final, | ||||||||||
| 29 | 'start_element' => \&_start_element, | ||||||||||
| 30 | }, | ||||||||||
| 31 | 'non_parser_options' => { | ||||||||||
| 32 | 'hist' => {}, | ||||||||||
| 33 | 'stack' => [], | ||||||||||
| 34 | }, | ||||||||||
| 35 | 10 | 'output_handler' => $self->{'output_handler'}, | |||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | # Object. | ||||||||||
| 39 | 10 | return $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | # Parse pyx text or array of pyx text. | ||||||||||
| 43 | sub parse { | ||||||||||
| 44 | 3 | ▶ | my ($self, $pyx, $out) = @_; | ||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 3 | $self->{'_pyx_parser'}->parse($pyx, $out); | |||||||||
| 46 | 1 | return; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Parse file with pyx text. | ||||||||||
| 50 | sub parse_file { | ||||||||||
| 51 | 3 | ▶ | my ($self, $file, $out) = @_; | ||||||||
|
Subroutine
parse_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 3 | $self->{'_pyx_parser'}->parse_file($file, $out); | |||||||||
| 53 | 1 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # Parse from handler. | ||||||||||
| 57 | sub parse_handler { | ||||||||||
| 58 | 3 | ▶ | my ($self, $input_file_handler, $out) = @_; | ||||||||
|
Subroutine
parse_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 3 | $self->{'_pyx_parser'}->parse_handler($input_file_handler, $out); | |||||||||
| 60 | 1 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # End of element. | ||||||||||
| 64 | sub _end_element { | ||||||||||
| 65 | 12 | ▶ | my ($pyx_parser_obj, $elem) = @_; | ||||||||
|
Subroutine
_end_element: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 12 | my $stack_ar = $pyx_parser_obj->{'non_parser_options'}->{'stack'}; | |||||||||
| 67 | 12 | ▶ | if ($stack_ar->[-1] eq $elem) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 9 | pop @{$stack_ar}; | |||||||||
| 69 | } else { | ||||||||||
| 70 | 3 | err 'Bad end of element.', | |||||||||
| 71 | 'Element', $elem; | ||||||||||
| 72 | } | ||||||||||
| 73 | 9 | return; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | # Finalize. | ||||||||||
| 77 | sub _final { | ||||||||||
| 78 | 6 | ▶ | my $pyx_parser_obj = shift; | ||||||||
|
Subroutine
_final: called CC=3 SCAR=11.0
| |||||||||||
| 79 | 6 | my $stack_ar = $pyx_parser_obj->{'non_parser_options'}->{'stack'}; | |||||||||
| 80 | 6 | ▶ | if (@{$stack_ar} > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 3 | err 'Stack has some elements.'; | |||||||||
| 82 | } | ||||||||||
| 83 | 3 | my $hist_hr = $pyx_parser_obj->{'non_parser_options'}->{'hist'}; | |||||||||
| 84 | 3 | ▶ | my $max_len = length reduce { length($a) > length($b) ? $a : $b } | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 85 | 3 | keys %{$hist_hr}; | |||||||||
| 86 | 3 | my $out = $pyx_parser_obj->{'output_handler'}; | |||||||||
| 87 | 3 | foreach my $key (sort keys %{$hist_hr}) { | |||||||||
| 88 | 6 | printf {$out} "[ %-${max_len}s ] %s\n", $key, $hist_hr->{$key}; | |||||||||
| 89 | } | ||||||||||
| 90 | 3 | return; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # Start of element. | ||||||||||
| 94 | sub _start_element { | ||||||||||
| 95 | 18 | ▶ | my ($pyx_parser_obj, $elem) = @_; | ||||||||
|
Subroutine
_start_element: called CC=2 SCAR=6.9
| |||||||||||
| 96 | 18 | my $stack_ar = $pyx_parser_obj->{'non_parser_options'}->{'stack'}; | |||||||||
| 97 | 18 | push @{$stack_ar}, $elem; | |||||||||
| 98 | 18 | ▶ | if (! $pyx_parser_obj->{'non_parser_options'}->{'hist'}->{$elem}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 15 | $pyx_parser_obj->{'non_parser_options'}->{'hist'}->{$elem} = 1; | |||||||||
| 100 | } else { | ||||||||||
| 101 | 3 | $pyx_parser_obj->{'non_parser_options'}->{'hist'}->{$elem}++; | |||||||||
| 102 | } | ||||||||||
| 103 | 18 | return; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 6 | 1; | |||||||||
| 107 | |||||||||||
| 108 | __END__ | ||||||||||