[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 319 - (download) (as text) (annotate)
Thu May 30 19:17:02 2002 UTC (17 years, 7 months ago) by sh002i
File size: 2554 byte(s)
takes symbols defined in config files (which are now perl code) as
course environment. ha HA!
-sam

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9