[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 422 - (download) (as text) (annotate)
Wed Jul 3 23:22:29 2002 UTC (10 years, 10 months ago) by sh002i
File size: 2998 byte(s)
moved hash2string and array2string from CourseEnvironment to Utils.
-sam

    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(runtime_use readFile formatDate hash2string array2string);
   11 
   12 sub runtime_use($) {
   13   return unless @_;
   14   eval "require $_[0]; import $_[0]";
   15   die $@ if $@;
   16 }
   17 
   18 sub readFile($) {
   19   my $fileName = shift;
   20   open INPUTFILE, "<", $fileName
   21     or die "Failed to read $fileName: $!";
   22   local $/ = undef;
   23   my $result = <INPUTFILE>;
   24   close INPUTFILE;
   25   return $result;
   26 }
   27 
   28 sub formatDateTime($) {
   29   my $dateTime = shift;
   30   # "standard" WeBWorK date/time format:
   31   # %m  month number, starting with 01
   32   # %d  numeric day of the month, with leading zeros (eg 01..31)
   33   # %y  year (2 digits)
   34   # %I  hour, 12 hour clock, leading 0's)
   35   # %M  minute, leading 0's
   36   # %P  am or pm (Yes %p and %P are backwards :)
   37   return time2str "%m/%d/%y %I:%M%P", $dateTime;
   38 }
   39 
   40 sub parseDateTime($) {
   41   $string = shift;
   42   return str2time $string;
   43 }
   44 
   45 sub 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 
   64 sub 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
   84 sub 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 
  111 sub 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 }
  121 sub 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 
  133 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9