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

Diff of /trunk/webwork2/lib/WeBWorK/CourseEnvironment.pm

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

Revision 311 Revision 319
1package WeBWorK::CourseEnvironment; 1package WeBWorK::CourseEnvironment;
2 2
3use strict;
4use warnings;
3use Safe; 5use Safe;
4 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
5sub new { 11sub new {
6 my $proto = shift; 12 my $invocant = shift;
7 my $class = ref($proto) || $proto; 13 my $class = ref($invocant) || $invocant;
8 my $webworkRoot = shift; 14 my $webworkRoot = shift;
9 my $courseName = 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'");
10 21
11 # determine location of globalEnvironmentFile 22 # determine location of globalEnvironmentFile
12 my $globalEnvironmentFile = "$webworkRoot/conf/global.conf"; 23 my $globalEnvironmentFile = "$webworkRoot/conf/global.conf";
13 24
14 # read and evaluate the global environment file 25 # read and evaluate the global environment file
15 my $globalFileContents = readFile($globalEnvironmentFile); 26 my $globalFileContents = readFile($globalEnvironmentFile);
16 my %globalConf = Safe->new->reval($globalFileContents); 27 $safe->reval($globalFileContents);
17 28
18 # if that evaluation failed, we can't really go on -- we need a global environment! 29 # if that evaluation failed, we can't really go on...
30 # we need a global environment!
19 $@ and die "Could not evaluate global environment file $globalEnvironmentFile: $@"; 31 $@ and die "Could not evaluate global environment file $globalEnvironmentFile: $@";
20 32
21 # determine location of courseEnvironmentFile 33 # determine location of courseEnvironmentFile
22 my $courseEnvironmentFile = $globalConf{coursesDirectory} 34 # pull it out of $safe's symbol table ad hoc
23 . "/$courseName/" 35 # (we don't want to do the hash conversion yet)
24 . $globalConf{courseEnvironmentFilename}; 36 no strict 'refs';
25 37 my $courseEnvironmentFile = ${*{${$safe->root."::"}{courseFiles}}}{environment};
38 use strict 'refs';
39
26 # read and evaluate the course environment file 40 # read and evaluate the course environment file
41 # if readFile failed, we don't bother trying to reval
27 my $courseFileContents = readFile($courseEnvironmentFile); 42 my $courseFileContents = eval { readFile($courseEnvironmentFile) }; # catch exceptions
28 my %courseConf = Safe->new->reval($courseFileContents); 43 $@ or $safe->reval($courseFileContents);
29
30 # if that evaluation failed, we can't really go on -- we need a course environment!
31 $@ and die "Could not evaluate course environment file $courseEnvironmentFile: $@";
32
33 my $self = { %globalConf, %courseConf };
34 44
35 # This comes in as a parameter to new(), not from any file. 45 # get the safe compartment's namespace as a hash
36 $self->{courseName} = $courseName; 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
37 bless $self, $class; 70 bless $self, $class;
38 return $self; 71 return $self;
39}
40
41sub get {
42 my $self = shift;
43 my $var = shift;
44 return $self->{$var};
45} 72}
46 73
47# ----- utils ----- 74# ----- utils -----
48 75
49sub readFile { 76sub readFile {
50 my $fileName = shift; 77 my $fileName = shift;
78 open INPUTFILE, "<", $fileName
51 open INPUTFILE, "<", $fileName or die "Couldn't open environment file $fileName: $!"; 79 or die "Couldn't open environment file $fileName: $!";
52 my $result = join "\n", <INPUTFILE>; 80 my $result = join "\n", <INPUTFILE>;
53 close INPUTFILE; 81 close INPUTFILE;
54 return $result; 82 return $result;
55} 83}
56 84

Legend:
Removed from v.311  
changed lines
  Added in v.319

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9