[system] / trunk / webwork2 / lib / WeBWorK / Utils.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WeBWorK/Utils.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 424 - (view) (download) (as text)

1 : sh002i 410 package WeBWorK::Utils;
2 :     use base qw(Exporter);
3 :    
4 : sh002i 412 use strict;
5 :     use warnings;
6 :     use Date::Format;
7 :     use Date::Parse;
8 :    
9 : sh002i 410 our @EXPORT = ();
10 : sh002i 424 our @EXPORT_OK = qw(
11 :     runtime_use
12 :     readFile
13 :     formatDateTime
14 :     parseDateTime
15 :     ref2string
16 :     hash2string
17 :     array2string
18 :     );
19 : sh002i 410
20 :     sub runtime_use($) {
21 :     return unless @_;
22 : sh002i 424 eval "package Main; require $_[0]; import $_[0]";
23 : sh002i 410 die $@ if $@;
24 :     }
25 :    
26 :     sub readFile($) {
27 :     my $fileName = shift;
28 :     open INPUTFILE, "<", $fileName
29 :     or die "Failed to read $fileName: $!";
30 :     local $/ = undef;
31 :     my $result = <INPUTFILE>;
32 :     close INPUTFILE;
33 :     return $result;
34 :     }
35 : sh002i 412
36 :     sub formatDateTime($) {
37 :     my $dateTime = shift;
38 :     # "standard" WeBWorK date/time format:
39 :     # %m month number, starting with 01
40 :     # %d numeric day of the month, with leading zeros (eg 01..31)
41 :     # %y year (2 digits)
42 :     # %I hour, 12 hour clock, leading 0's)
43 :     # %M minute, leading 0's
44 :     # %P am or pm (Yes %p and %P are backwards :)
45 :     return time2str "%m/%d/%y %I:%M%P", $dateTime;
46 :     }
47 :    
48 :     sub parseDateTime($) {
49 : sh002i 424 my $string = shift;
50 : sh002i 412 return str2time $string;
51 :     }
52 : sh002i 422
53 : sh002i 424 # -----
54 : sh002i 422
55 : sh002i 424 sub ref2string($;$);
56 :     sub ref2string($;$) {
57 :     my $ref = shift;
58 :     my $dontExpand = shift || {};
59 :     my $refType = ref $ref;
60 : sh002i 422 my $result;
61 : sh002i 424 if ($refType and not $dontExpand->{$refType}) {
62 :     my $baseType = refBaseType($ref);
63 :     $result .= '<font size="1" color="grey">' . $refType;
64 :     $result .= " ($baseType)" if $refType ne $baseType;
65 :     $result .= ":</font><br>";
66 :     $result .= '<table border="1" cellpadding="2">';
67 :     if ($baseType eq "HASH") {
68 :     my %hash = %$ref;
69 :     foreach (sort keys %hash) {
70 :     $result .= '<tr valign="top">';
71 :     $result .= "<td>$_</td>";
72 :     $result .= "<td>" . ref2string($hash{$_}, $dontExpand) . "</td>";
73 :     $result .= "</tr>";
74 :     }
75 :     } elsif ($baseType eq "ARRAY") {
76 :     my @array = @$ref;
77 :     foreach (0 .. $#array) {
78 :     $result .= '<tr valign="top">';
79 :     $result .= "<td>$_</td>";
80 :     $result .= "<td>" . ref2string($array[$_], $dontExpand) . "</td>";
81 :     $result .= "</tr>";
82 :     }
83 :     } elsif ($baseType eq "SCALAR") {
84 :     my $scalar = $$ref;
85 :     $result .= '<tr valign="top">';
86 :     $result .= "<td>$scalar</td>";
87 :     $result .= "</tr>";
88 : sh002i 422 } else {
89 : sh002i 424 # perhaps a coderef? in any case, i don't feel like dealing with it!
90 :     $result .= '<tr valign="top">';
91 :     $result .= "<td>$ref</td>";
92 :     $result .= "</tr>";
93 : sh002i 422 }
94 : sh002i 424 $result .= "</table>"
95 : sh002i 422 } else {
96 : sh002i 424 $result .= defined $ref ? $ref : '<font color="red">undef</font>';
97 :     }
98 : sh002i 422 }
99 :    
100 : sh002i 424 sub refBaseType($) {
101 :     my $ref = shift;
102 :     local $SIG{__DIE__} = 'IGNORE';
103 :     return "CODE" if eval { $_ = \&$ref; 1 };
104 :     return "HASH" if eval { $_ = %$ref; 1 };
105 :     return "ARRAY" if eval { $_ = @$ref; 1 };
106 :     return "SCALAR" if eval { $_ = $$ref; 1 };
107 :     return 0;
108 : sh002i 422 }
109 :    
110 : sh002i 424 # -----
111 :    
112 :     #sub hash2string($;$$) {
113 :     # my $hr = shift;
114 :     # my $table = shift || 0;
115 :     # my $indent = shift || 0;
116 :     # my $result = $table ? '<table border="1">' : "";
117 :     # foreach my $key (keys %$hr) {
118 :     # my $value = $hr->{$key};
119 :     # $result .= $table
120 :     # ? "<tr><td>$key</td>"
121 :     # : "\t"x$indent . "{$key} =";
122 :     # if (ref $value eq 'HASH') {
123 :     # $result .= $table ? "<td>" : "\n";
124 :     # $result .= hash2string($value, $table, $indent+1);
125 :     # $result .= $table ? "</td>" : "";
126 :     # } elsif (ref $value eq 'ARRAY') {
127 :     # $result .= $table ? "<td>" : "\n";
128 :     # $result .= array2string($value, $table, $indent+1);
129 :     # $result .= $table ? "</td>" : "";
130 :     # } elsif (defined $value) {
131 :     # $result .= $table
132 :     # ? "<td>$value</td>"
133 :     # : " $value\n";
134 :     # } else {
135 :     # $result .= $table ? "" : "\n";
136 :     # }
137 :     # $result .= $table ? "</tr>" : "";
138 :     # }
139 :     # $result .= "</table>";
140 :     # return $result;
141 :     #}
142 :     #
143 :     #sub array2string($;$$) {
144 :     # my $ar = shift;
145 :     # my $table = shift || 0;
146 :     # my $indent = shift || 0;
147 :     # my $result = $table ? '<table border="1">' : "";
148 :     # foreach my $index (0 .. @$ar-1) {
149 :     # my $value = $ar->[$index];
150 :     # $result .= $table
151 :     # ? "<tr><td>$index</td>"
152 :     # : "\t"x$indent . "[$index] =";
153 :     # if (ref $value eq 'HASH') {
154 :     # $result .= $table ? "<td>" : "\n";
155 :     # $result .= hash2string($value, $table, $indent+1);
156 :     # $result .= $table ? "</td>" : "";
157 :     # } elsif (ref $value eq 'ARRAY') {
158 :     # $result .= $table ? "<td>" : "\n";
159 :     # $result .= array2string($value, $table, $indent+1);
160 :     # $result .= $table ? "</td>" : "";
161 :     # } elsif (defined $value) {
162 :     # $result .= $table
163 :     # ? "<td>$value</td>"
164 :     # : " $value\n";
165 :     # } else {
166 :     # $result .= $table ? "" : "\n";
167 :     # }
168 :     # $result .= $table ? "</tr>" : "";
169 :     # }
170 :     # $result .= "</table>";
171 :     # return $result;
172 :     #}
173 :     #
174 :     #sub isHashRef($) {
175 :     # my $ref = shift;
176 :     # local $SIG{__DIE__} = 'IGNORE';
177 :     # $_ = eval{ %$ref };
178 :     # return not defined $@;
179 :     #}
180 :     #
181 :     #sub isArrayRef($) {
182 :     # my $ref = shift;
183 :     # local $SIG{__DIE__} = 'IGNORE';
184 :     # $_ = eval{ @$ref };
185 :     # return not defined $@;
186 :     #}
187 :    
188 : sh002i 422 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9