Genetics/Util.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 149 CC 16 SCAR 36.4
CC
16
Cov
0%
browse
56.0
now
42.0
structure2Str
30.0
CRAP 38.1
1 # GenPerl module
2 #
3  
4 package Genetics::Util ;
5  
6 require Exporter ;
7 use strict ;
8  
9 use vars qw($ID $VERSION @ISA @EXPORT) ;
10 $ID = "SLM::Lib" ;
11 $VERSION = "0.01" ;
12 @ISA = qw(Exporter) ;
13 @EXPORT = qw(now browse structure2Str) ;
14  
15  
16 =head1 NAME
17  
18 SLM::Lib
19  
20 =head1 SYNOPSIS
21  
22 Not really appropriate. See individual functions.
23  
24 =head1 DESCRIPTION
25  
26 A collection of GenPerl utility functions.
27  
28 =head1 LICENSE
29  
30 This program is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 the Free Software Foundation; either version 2 of the License, or
33 (at your option) any later version.
34  
35 This program is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 GNU General Public License for more details.
39  
40 You should have received a copy of the GNU General Public License
41 along with this program; if not, write to the Free Software
42 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43  
44 =head1 FEEDBACK
45  
46 Currently, all feedback should be sent directly to the author.
47  
48 =head1 AUTHOR - Steve Mathias
49  
50 Email: mathias@genomica.com
51  
52 Phone: (720) 565-4029
53  
54 Address: Genomica Corporation
55 1745 38th Street
56 Boulder, CO 80301
57  
58 =head1 DETAILS
59  
60 The rest of the documentation describes each of the object variables and
61 methods. The names of internal variables and methods are preceded with an
62 underscore (_).
63  
64 =head2 now
65  
66 Function : Generate a text representation of the current date/time
67 Arguments : Hash
68 Returns : Scalar text string
69 Example : print LOG now(format => 'fancy')
70 Scope : Public
71 Comments : Formats: raw : comma-delimited output from localtime(time)
72 simple : "1965-11-14 12:45:44"
73 dayonly : "14 November 1965"
74 fancy : "Friday November 14, 1965 12:45:44"
75 mysqldate : "1965-11-14"
76  
77 =cut
78  
79 sub now {
80 my(%args) = @_ ;
81 my $format = $args{format} || "raw" ;
82  
83 my @textDays = qw(Sunday Monday Tuesday Wednesday
84 Thursday Friday Saturday) ;
85 my @textMonths = qw(January February March April MAy June July
86 August September October November December) ;
87 my($textDay, $textMonth, $now) ;
88  
89 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time) ;
90  
91 $year += 1900 unless $format eq "raw" ;
92  
93 if ($format eq "mysqldate") {
94 $mon++ ;
95 ($mon < 10) and ($mon = "0" . $mon) ;
96 ($mday < 10) and ($mday = "0" . $mday) ;
97 $now = "$year-$mon-$mday" ;
98 } elsif ($format eq "simple") {
99 $mon++ ;
100 ($mon < 10) and ($mon = "0" . $mon) ;
101 ($mday < 10) and ($mday = "0" . $mday) ;
102 $now = "$year-$mon-$mday $hour:$min:$sec" ;
103 } elsif ($format eq "fancy") {
104 $textDay = $textDays[$wday] ;
105 $textMonth = $textMonths[$mon] ;
106 $now = "$textDay $textMonth $mday, $year $hour:$min:$sec" ;
107 } elsif ($format eq "dayonly") {
108 $textMonth = $textMonths[$mon] ;
109 $now = "$mday $textMonth $year" ;
110 } else {
111 # raw format
112 $now = "$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst" ;
113 }
114  
115 return $now ;
116 }
117  
118 =head2 browse
119  
120 Function : A simple de-referencing browser for perl data structures
121 Arguments : Scalar
122 Returns : N/A
123 Example : browse()
124 Scope : Public
125 Comments : See also structure2Str()
126  
127 =cut
128  
129 sub browse {
130 my ($val, $level) = @_ ;
131 $level = 0 unless defined $level ;
132 my $tab = ' ' x $level ;
133 my $ref = ref $val ;
134
135 if (not defined $val) {
136 print "undef\n" ;
137 } elsif ($ref eq 'CODE' or $ref eq 'GLOB') {
138 print $val, "\n" ;
139 } elsif ($ref eq 'ARRAY') {
140 print "$ref\n" ;
141 $level++ ;
142 for(my $i = 0; $i < @{$val}; $i++) {
143 print "$tab [$i] = " ;
144 browse($val->[$i], $level) ;
145 }
146 } elsif ($ref eq 'HASH' or $ref =~ /^[A-Z]/) {
147 print "$ref\n" ;
148 $level++ ;
149 foreach my $key (keys %{$val}) {
150 print "$tab $key => " ;
151 browse($val->{$key}, $level) ;
152 }
153 } elsif ($ref) {
154 print $ref, "\n" ;
155 } else {
156 print $val, "\n" ;
157 }
158 }
159  
160 =head2 structure2Str
161  
162 Function : Convert a pointer to a complex perl data structure into a string
163 for printing.
164 Arguments : Scalar
165 Returns : N/A
166 Example : browse()
167 Scope : Private Class Method
168 Comments : Recurses if nested pointers exist in the data structure.
169  
170 =cut
171  
172 sub structure2Str {
173 my($value) = @_ ;
174 my($str, $k, $v, @data) ;
175  
176 if (not defined $value) {
177 $str = "" ;
178 } elsif (not ref $value) {
179 $str = $value ;
180 } elsif (ref $value eq "ARRAY") {
181 foreach $v (@$value) {
182 $str = _attr2String($v) ;
183 push(@data, $str)
184 }
185 $str = "[ " . join(", ", @data) . " ]" ;
186
187 } elsif (ref $value eq "HASH") {
188 @data = () ;
189 while (($k,$v) = each %$value) {
190 $str = _attr2String($v) ;
191 push(@data, "$k => $str") ;
192 }
193 $str = "{ " . join(", ", @data) . " }" ;
194 } else {
195 $str = "Reference to an unsupported type" ;
196 }
197  
198 return($str) ;
199 }
200 1;
201