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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 424 - (download) (as text) (annotate)
Thu Jul 11 19:09:08 2002 UTC (10 years, 10 months ago) by sh002i
File size: 4607 byte(s)
Problem.pm/PG.pm/Translator.pm now compile and work (to some degree)
changed the format of pg/modules in global.conf
diddled with the format of system.template (i believe i moved an <HR>)
added ref2string function to Utils.pm, removed hash2string/array2string
fixed a package name in IO.pm

    1 package WeBWorK::Utils;
    2 use base qw(Exporter);
    3 
    4 use strict;
    5 use warnings;
    6 use Date::Format;
    7 use Date::Parse;
    8 
    9 our @EXPORT    = ();
   10 our @EXPORT_OK = qw(
   11   runtime_use
   12   readFile
   13   formatDateTime
   14   parseDateTime
   15   ref2string
   16   hash2string
   17   array2string
   18 );
   19 
   20 sub runtime_use($) {
   21   return unless @_;
   22   eval "package Main; require $_[0]; import $_[0]";
   23   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 
   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   my $string = shift;
   50   return str2time $string;
   51 }
   52 
   53 # -----
   54 
   55 sub ref2string($;$);
   56 sub ref2string($;$) {
   57   my $ref = shift;
   58   my $dontExpand = shift || {};
   59   my $refType = ref $ref;
   60   my $result;
   61   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     } else {
   89       # 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     }
   94     $result .= "</table>"
   95   } else {
   96     $result .= defined $ref ? $ref : '<font color="red">undef</font>';
   97   }
   98 }
   99 
  100 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 }
  109 
  110 # -----
  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 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9