File Coverage

blib/lib/NanoB2B/UniversalRoutines.pm
Criterion Covered Total %
statement 18 135 13.3
branch 0 20 0.0
condition n/a
subroutine 6 24 25.0
pod 0 16 0.0
total 24 195 12.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # NanoB2B::UniversalRoutines
3             #
4             # Just universally used subroutines (i.e. printColor, inArray, wordIndex)
5             # Version 1.0
6             #
7             # Program by Milk
8              
9              
10             package NanoB2B::UniversalRoutines;
11              
12             ####################### IMPORTS ####################
13              
14 1     1   561 use Term::ANSIColor; #color coding the output
  1         12882  
  1         100  
15 1     1   645 use List::MoreUtils qw(first_index); #check for first occurrence of a word
  1         11816  
  1         7  
16 1     1   941 use List::MoreUtils qw(indexes); #get all of the indexes of a word
  1         2  
  1         3  
17 1     1   556 use File::Path qw(make_path); #makes sub directories
  1         2  
  1         93  
18              
19 1     1   6 use strict;
  1         2  
  1         15  
20 1     1   5 use warnings;
  1         1  
  1         1343  
21              
22             ##### GLOBAL VARIABLES #####
23              
24             my $debug = 0;
25              
26             #----------------------------------------
27             # constructor
28             #----------------------------------------
29             # constructor method to create a new UR object
30             # input : $params <- reference to hash containing the parameters
31             # output: $self <- a UR object
32              
33             sub new {
34             #grab class and parameters
35 0     0 0   my $self = {};
36 0           my $class = shift;
37 0 0         return undef if(ref $class);
38 0           my $params = shift;
39              
40             #bless this object
41 0           bless $self, $class;
42              
43             # get some of the parameters
44 0           my $debugoption = $params->{'debug'};
45 0 0         if(defined $debugoption){$debug = $debugoption;}
  0            
46              
47 0           return $self;
48             }
49              
50             ################# HERE BE SUB-ROUTINES ######################
51             ################# (alphabatized for your convenience :D) ######################
52              
53              
54             #excludes a value from a number array
55             # input : $e <-- the range of elements in the array from 1-$e ($e=5 => 1,2,3,4,5)
56             # $bully <-- the number to exclude from the set (3 => 1,2,4,5)
57             # output : @kids <-- the number set
58             sub bully{
59 0     0 0   my $self = shift;
60 0           my $num = shift;
61 0           my $bully = shift;
62              
63 0           my @kids = (1..$num);
64 0           @kids = grep { $_ != $bully } @kids;
  0            
65 0           return @kids;
66             }
67              
68             # cleans the line without getting rid of tags
69             # input : $input <-- the line to clean up
70             # output : $input <-- cleaned up input line
71             sub cleanWords{
72 0     0 0   my $self = shift;
73 0           my $input = shift;
74              
75 0           $input = lc($input);
76 0           $input =~ s/[^a-zA-z0-9\:\.\s<>&#;\*\/]/ /og; #get rid of non-ascii
77             #$input =~ s/([0-9]+(\.[0-9]*)?)-[0-9]+(\.[0-9]*)?/RANGE/g; #get rid of range num (#-#)
78 0           $input =~ s/[0-9]+(.[0-9]+)?/NUM/og; #get rid of normal num (#.#)
79 0           $input =~ s/\s?=\s?/eq/og; #get rid of =
80 0           $input =~ s///og; #get rid of
81 0           $input =~ s/[\*\/]//og; #get rid of * and /
82             #$input =~ s/[,\)\(\\\'\/\=\*\-]/ /g;
83 0           $input =~ s/\s\+/_/og; #get rid of _+ space
84 0           $input =~ s/\s+\.\s+/ /og; #get rid of _._ periods
85 0           $input =~ s/\.\s+/ /og; #get rid of ._ space
86 0           $input =~ s/\s+/ /og; #get rid of excessive blank space
87 0           return $input;
88             }
89              
90             #helper function that retrieves all of the indexes of a word in a given set
91             # input : $word <-- an element or object
92             # @set <-- the array to look through
93             # output : array <-- returns a set of all indexes of the word; returns {-1} if none found
94             sub getAllIdxs{
95 0     0 0   my $self = shift;
96 0           my $word = shift;
97 0           my $set_ref = shift;
98 0           my @set = @$set_ref;
99              
100 0     0     my @idxs = indexes{$_ eq $word} @set;
  0            
101 0           return @idxs;
102             }
103              
104             #gets the line's index
105             # input : $keyword <-- the regex to use to search for the specific line
106             # @lines <-- the set of lines to look through
107             # output : $a <-- return the index of the line based on the regex; returns -1 if not found
108             sub getIndexofLine{
109 0     0 0   my $self = shift;
110 0           my $keyword = shift;
111 0           my $lines_ref = shift;
112 0           my @lines = @$lines_ref;
113              
114 0           my $len = @lines;
115 0           for(my $a = 0; $a < $len; $a++){
116 0           my $line = $lines[$a];
117 0 0         if ($line =~ /($keyword)/){ #regex checking if string contains keyword
118 0           return $a;
119             }
120             }
121 0           return -1;
122             }
123              
124             #helper function to check if an element is in an array
125             # input : $e <-- an element or object
126             # @array <-- the array to look through
127             # output : boolean <-- 1 if it is in the array, 0 if it isn't
128             sub inArr{
129 0     0 0   my $self = shift;
130 0           my $e = shift;
131 0           my $arr_ref = shift;
132 0           my @arr = @$arr_ref;
133              
134 0     0     my $ans = first_index {$_ eq $e} @arr;
  0            
135 0 0         if($ans > -1){
136 0           return 1;
137             }else{
138 0           return 0;
139             }
140             }
141              
142             #prints to a file called debug
143             # input : $output <-- the text to output to the debug file
144             # output : a local file named 'debug'
145             sub print2DebugFile{
146 0     0 0   my $self = shift;
147 0           my $output = shift;
148              
149 0 0         open(DEBUG, ">>", "debug") || die "**ERROR: Unable to create debug file!**\n$!";
150 0           print2File(, $output);
151             }
152              
153             #prints to a file w/ line skip
154             # input : $file <-- the file to print to (must already be opened!)
155             # @array <-- the text to print to the file (includes next line)
156             # output : the file with the text printed to it (with a return character)
157             sub print2File{
158 0     0 0   my $self = shift;
159 0           my $file = shift;
160 0           my $txt = shift;
161              
162 0           print $file "$txt\n";
163             }
164              
165             #prints to a file as is
166             # input : $file <-- the file to print to (must already be opened!)
167             # @array <-- the text to print to the file
168             # output : the file with the text printed to it without a return character
169             sub print2FileNoLine{
170 0     0 0   my $self = shift;
171 0           my $file = shift;
172 0           my $txt = shift;
173              
174 0           print $file "$txt";
175             }
176              
177             #shows an array
178             # input : $delim <-- string to separate the elememts by
179             # @array <-- the array to print
180             # output : string <-- returns the array elements in a string format separated by the delimiter
181             sub printArr{
182 0     0 0   my $self = shift;
183 0           my $delim = shift;
184 0           my $parr_ref = shift;
185 0           my @parr = @$parr_ref;
186              
187 0           my $combo = join ($delim, @parr);
188 0           print "$combo\n";
189             }
190              
191              
192             #prints input with color
193             # input : $color <-- color to print the text in (ex. 'red', 'bold blue', 'on_green')
194             # $text <-- the text to print
195             # output : --
196             sub printColor{
197 0     0 0   my $self = shift;
198 0           my $color = shift;
199 0           my $text = shift;
200              
201 0 0         if($color =~ /on_\w+/){print color($color), "$text", color("reset"), "\n";}
  0            
202 0           else{print color($color), "$text", color("reset");}
203             }
204              
205             #prints input with color for debug mode only
206             # input : $color <-- color to print the text in (ex. 'magenta', 'bright_cyan', 'on_bright_yellow')
207             # $text <-- the text to print
208             # output : --
209             sub printColorDebug{
210 0     0 0   my $self = shift;
211 0           my $color = shift;
212 0           my $text = shift;
213              
214 0 0         if($debug){
215 0 0         if($color =~ /on_\w+/){print color($color), "$text", color("reset"), "\n";}
  0            
216 0           else{print color($color), "$text", color("reset");}
217            
218             }
219             }
220              
221             #prints only if debug mode is on
222             # input : $text <-- the text to print
223             # output : --
224             sub printDebug{
225 0     0 0   my $self = shift;
226 0           my $text = shift;
227              
228 0 0         if($debug){
229 0           print ($text);
230             }
231             }
232              
233             #helper function that checks if an array set is in another array
234             # input : @arr1 <-- the subject array element
235             # @arr2 <-- the array to look through
236             # output : boolean <-- 1 if it is in the array, 0 if it isn't
237             sub setInArr{
238 0     0 0   my $self = shift;
239 0           my $arr1_ref = shift;
240 0           my $arr2_ref = shift;
241            
242 0           my @arr1 = @$arr1_ref;
243 0           my @arr2 = @$arr2_ref;
244              
245             #combine the arrays as a string
246 0           my $str1 = join " ", @arr1;
247 0           my $str2 = join " ", @arr2;
248              
249             #check for the substring of arr2 in arr1
250 0 0         if(index($str2, $str1) != -1){
251 0           return 1;
252             }else{
253 0           return 0;
254             }
255             }
256              
257             #counts how many times a word appears in a set
258             # input : $word <-- the word to look for in the array
259             # @arr <-- the array to look through
260             # output : $num <-- the total number of times the element occurs in the array
261             sub wordCount{
262 0     0 0   my $self = shift;
263 0           my $word = shift;
264 0           my $arr_ref = shift;
265 0           my @arr = @$arr_ref;
266              
267 0           my @idx = getAllIdxs($word, @arr);
268 0           my $num = @idx;
269 0           return $num;
270             }
271              
272             #makes a counter for each word in a line
273             # input : @words <-- the set of words to count
274             # output : @index_set <-- the set of counter numbers correlating to each word
275             sub wordIndex{
276 0     0 0   my $self = shift;
277 0           my $words_ref = shift;
278 0           my @words = @$words_ref;
279              
280 0           my @curWords = ();
281 0           my @index_set = ();
282             #for each word in the set count it's frequency
283 0           foreach my $word (@words){
284 0           my $index = wordCount($word, @curWords) + 1;
285 0           push(@curWords, $word);
286 0           push (@index_set, $index);
287             }
288              
289 0           return @index_set;
290             }
291              
292             1;