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

View of /trunk/webwork2/lib/WeBWorK/CourseEnvironment.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: 2363 byte(s)
moved hash2string and array2string from CourseEnvironment to Utils.
-sam

    1 package WeBWorK::CourseEnvironment;
    2 
    3 use strict;
    4 use warnings;
    5 use Safe;
    6 use WeBWorK::Utils qw(readFile);
    7 
    8 # new($invocant, $webworkRoot, $courseName)
    9 # $invocant implicitly set by caller
   10 # $webworkRoot  directory that contains the WeBWorK distribution
   11 # $courseName name of the course being used
   12 sub new {
   13   my $invocant = shift;
   14   my $class = ref($invocant) || $invocant;
   15   my $webworkRoot = shift;
   16   my $courseName = shift;
   17   my $safe = Safe->new;
   18 
   19   # set up some defaults that the environment files will need
   20   $safe->reval("\$webworkRoot = '$webworkRoot'");
   21   $safe->reval("\$courseName = '$courseName'");
   22 
   23   # determine location of globalEnvironmentFile
   24   my $globalEnvironmentFile = "$webworkRoot/conf/global.conf";
   25 
   26   # read and evaluate the global environment file
   27   my $globalFileContents = readFile($globalEnvironmentFile);
   28   $safe->reval($globalFileContents);
   29 
   30   # if that evaluation failed, we can't really go on...
   31   # we need a global environment!
   32   $@ and die "Could not evaluate global environment file $globalEnvironmentFile: $@";
   33 
   34   # determine location of courseEnvironmentFile
   35   # pull it out of $safe's symbol table ad hoc
   36   # (we don't want to do the hash conversion yet)
   37   no strict 'refs';
   38   my $courseEnvironmentFile = ${*{${$safe->root."::"}{courseFiles}}}{environment};
   39   use strict 'refs';
   40 
   41   # read and evaluate the course environment file
   42   # if readFile failed, we don't bother trying to reval
   43   my $courseFileContents = eval { readFile($courseEnvironmentFile) }; # catch exceptions
   44   $@ or $safe->reval($courseFileContents);
   45 
   46   # get the safe compartment's namespace as a hash
   47   no strict 'refs';
   48   my %symbolHash = %{$safe->root."::"};
   49   use strict 'refs';
   50 
   51   # convert the symbol hash into a hash of regular variables.
   52   my $self = {};
   53   foreach my $name (keys %symbolHash) {
   54     # weed out internal symbols
   55     next if $name =~ /^(INC|_|__ANON__|main::)$/;
   56     # pull scalar, array, and hash values for this symbol
   57     my $scalar = ${*{$symbolHash{$name}}};
   58     my @array = @{*{$symbolHash{$name}}};
   59     my %hash = %{*{$symbolHash{$name}}};
   60     # for multiple variables sharing a symbol, scalar takes precedence
   61     # over array, which takes precedence over hash.
   62     if (defined $scalar) {
   63       $self->{$name} = $scalar;
   64     } elsif (@array) {
   65       $self->{$name} = \@array;
   66     } elsif (%hash) {
   67       $self->{$name} = \%hash;
   68     }
   69   }
   70 
   71   bless $self, $class;
   72   return $self;
   73 }
   74 
   75 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9