File Coverage

blib/lib/Data/FreqConvert.pm
Criterion Covered Total %
statement 52 54 96.3
branch 5 8 62.5
condition n/a
subroutine 9 9 100.0
pod 0 3 0.0
total 66 74 89.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Data::FreqConvert - converts variables to scalars holfding frequencys of keys in values
4              
5             =head1 VERSION
6              
7             Version 0.01
8              
9              
10             =head1 SYNOPSIS
11              
12             use Data::FreqConvert;
13             use Data::Printer;
14              
15             my $data = Data::FreqConvert->new();
16              
17             my %a = ("a"=>1,"b"=>1,"c"=>1);
18             my $a = {"a"=>1,"b"=>1,"c"=>1,"a"=>3};
19             my @a = ("a","b","c","a");
20             my $b = "a\nb\nc\nc";
21              
22             my $r = $data->freq($b);
23             p $r;
24              
25             $r = $data->freq(\@a);
26             p $r;
27              
28             $r = $data->freq($a);
29             p $r;
30              
31             =cut
32              
33             package Data::FreqConvert;
34              
35 2     2   42314 use strict;
  2         5  
  2         54  
36 2     2   9 use warnings;
  2         4  
  2         57  
37              
38 2     2   1708 use Data::Freq;
  2         53924  
  2         64  
39 2     2   26400 use Data::Dumper;
  2         21595  
  2         152  
40 2     2   2395 use Data::Printer;
  2         244952  
  2         17  
41 2     2   38041 use IO::Capture::Stdout;
  2         7815  
  2         1244  
42             our $VERSION = "0.01";
43              
44             sub new {
45 1     1 0 842 my $class = shift;
46 1         3 my (%params) = @_;
47              
48 1         3 my $self = {};
49 1         4 bless $self, $class;
50              
51 1         3 return $self;
52             }
53              
54              
55             sub prepArg {
56 2     2 0 3 my $self = shift;
57 2         5 my ($arg) = @_;
58 2         4 my $ref = ref $arg;
59 2         4 my @return = ();
60              
61 2 50       8 if($ref =~ /HASH/) {
62 0         0 @return = keys %$arg;
63             }
64 2 50       5 if($ref =~ /ARRAY/) {
65 0         0 @return = @$arg;
66             }
67              
68 2 50       7 if($ref eq "") {
69 2         9 @return = split("\n",$arg);
70             }
71              
72 2         8 return @return;
73              
74             }
75              
76              
77             sub freq {
78 2     2 0 15 my $self = shift;
79 2         4 my $arg = shift;
80 2         8 my @set = $self->prepArg($arg);
81 2         15 my $data = Data::Freq->new();
82 2         192 foreach my $n( @set){
83 5         262 $data->add($n);
84             }
85              
86 2         225 my $capture = IO::Capture::Stdout->new;
87 2         47 $capture->start;
88 2         137 $data->output();
89 2         604 $capture->stop();
90              
91 2         80 my $ret = {};
92 2         10 my @buffer = reverse grep{/\w|\d/}$capture->read;
  24         94  
93              
94 2         6 my $last = "";
95 2         5 foreach my $z(@buffer){
96 8 100       26 if($z !~ m/\d/) {
97 4         10 $ret->{$z}="";
98             }
99             else {
100 4         8 $ret->{$last}=$z;
101             }
102              
103 8         13 $last = $z;
104              
105             }
106              
107 2         24 return $ret;
108             }
109              
110             =head1 AUTHOR
111              
112             Mahiro Ando, C<< >>
113              
114             =head1 LICENSE AND COPYRIGHT
115              
116             Copyright 2015 Hagen Geissler
117              
118             This program is free software; you can redistribute it and/or modify it
119             under the terms of either: the GNU General Public License as published
120             by the Free Software Foundation; or the Artistic License.
121              
122             See http://dev.perl.org/licenses/ for more information.
123              
124             =cut
125              
126             1;