[system] / trunk / webwork-modperl / lib / WeBWorK / Utils.pm Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/lib/WeBWorK/Utils.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 421 Revision 422
5use warnings; 5use warnings;
6use Date::Format; 6use Date::Format;
7use Date::Parse; 7use Date::Parse;
8 8
9our @EXPORT = (); 9our @EXPORT = ();
10our @EXPORT_OK = qw(runtime_use readFile formatDate); 10our @EXPORT_OK = qw(runtime_use readFile formatDate hash2string array2string);
11 11
12sub runtime_use($) { 12sub runtime_use($) {
13 return unless @_; 13 return unless @_;
14 eval "require $_[0]; import $_[0]"; 14 eval "require $_[0]; import $_[0]";
15 die $@ if $@; 15 die $@ if $@;
39 39
40sub parseDateTime($) { 40sub parseDateTime($) {
41 $string = shift; 41 $string = shift;
42 return str2time $string; 42 return str2time $string;
43} 43}
44
45sub hash2string {
46 my $hr = shift;
47 my $indent = shift || 0;
48 my $result;
49 foreach (keys %$hr) {
50 $result .= "\t"x$indent . "{$_} =";
51 if (ref $hr->{$_} eq 'HASH') {
52 $result .= "\n";
53 $result .= hash2string($hr->{$_}, $indent+1);
54 } elsif (ref $hr->{$_} eq 'ARRAY') {
55 $result .= "\n";
56 $result .= array2string($hr->{$_}, $indent+1);
57 } else {
58 $result .= " " . $hr->{$_} . "\n";
59 }
60 }
61 return $result;
62}
63
64sub array2string {
65 my $ar = shift;
66 my $indent = shift || 0;
67 my $result;
68 foreach (0 .. @$ar-1) {
69 $result .= "\t"x$indent . "[$_] =";
70 if (ref $ar->[$_] eq 'HASH') {
71 $result .= "\n";
72 $result .= hash2string($ar->[$_], $indent+1);
73 } elsif (ref $ar->[$_] eq 'ARRAY') {
74 $result .= "\n";
75 $result .= array2string($ar->[$_], $indent+1);
76 } else {
77 $result .= " " . $ar->[$_] . "\n";
78 }
79 }
80 return $result;
81}
82
83=pod
84sub pretty_print_rh {
85 my $r_input = shift;
86 my $out = '';
87 if ( not ref($r_input) ) {
88 $out = $r_input; # not a reference
89 } elsif (is_hash_ref($r_input)) {
90 local($^W) = 0;
91 $out .= "<TABLE border = \"2\" cellpadding = \"3\" BGCOLOR = \"#FFFFFF\">";
92 foreach my $key (sort keys %$r_input ) {
93 $out .= "<tr><TD> $key</TD><TD>=&gt;</td><td>&nbsp;".pretty_print_rh($r_input->{$key}) . "</td></tr>";
94 }
95 $out .="</table>";
96 } elsif (is_array_ref($r_input) ) {
97 my @array = @$r_input;
98 $out .= "( " ;
99 while (@array) {
100 $out .= pretty_print_rh(shift @array) . " , ";
101 }
102 $out .= " )";
103 } elsif (ref($r_input) eq 'CODE') {
104 $out = "$r_input";
105 } else {
106 $out = $r_input;
107 }
108 $out;
109}
110
111sub is_hash_ref {
112 my $in =shift;
113 my $save_SIG_die_trap = $SIG{__DIE__};
114 $SIG{__DIE__} = sub {CORE::die(@_) };
115 my $out = eval{ %{ $in } };
116 $out = ($@ eq '') ? 1 : 0;
117 $@='';
118 $SIG{__DIE__} = $save_SIG_die_trap;
119 $out;
120}
121sub is_array_ref {
122 my $in =shift;
123 my $save_SIG_die_trap = $SIG{__DIE__};
124 $SIG{__DIE__} = sub {CORE::die(@_) };
125 my $out = eval{ @{ $in } };
126 $out = ($@ eq '') ? 1 : 0;
127 $@='';
128 $SIG{__DIE__} = $save_SIG_die_trap;
129 $out;
130}
131=cut
132
1331;

Legend:
Removed from v.421  
changed lines
  Added in v.422

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9