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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 319 - (view) (download) (as text)

1 : malsyned 283 package WeBWorK::CourseEnvironment;
2 :    
3 : sh002i 319 use strict;
4 :     use warnings;
5 : malsyned 283 use Safe;
6 :    
7 : sh002i 319 # 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 : malsyned 283 sub new {
12 : sh002i 319 my $invocant = shift;
13 :     my $class = ref($invocant) || $invocant;
14 : malsyned 283 my $webworkRoot = shift;
15 :     my $courseName = shift;
16 : sh002i 319 my $safe = Safe->new;
17 : malsyned 283
18 : sh002i 319 # set up some defaults that the environment files will need
19 :     $safe->reval("\$webworkRoot = '$webworkRoot'");
20 :     $safe->reval("\$courseName = '$courseName'");
21 :    
22 : malsyned 283 # determine location of globalEnvironmentFile
23 :     my $globalEnvironmentFile = "$webworkRoot/conf/global.conf";
24 : sh002i 319
25 : malsyned 283 # read and evaluate the global environment file
26 :     my $globalFileContents = readFile($globalEnvironmentFile);
27 : sh002i 319 $safe->reval($globalFileContents);
28 :    
29 :     # if that evaluation failed, we can't really go on...
30 :     # we need a global environment!
31 : malsyned 283 $@ and die "Could not evaluate global environment file $globalEnvironmentFile: $@";
32 :    
33 :     # determine location of courseEnvironmentFile
34 : sh002i 319 # 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 : malsyned 283 # read and evaluate the course environment file
41 : sh002i 319 # if readFile failed, we don't bother trying to reval
42 :     my $courseFileContents = eval { readFile($courseEnvironmentFile) }; # catch exceptions
43 :     $@ or $safe->reval($courseFileContents);
44 : malsyned 304
45 : sh002i 319 # 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 : malsyned 283 bless $self, $class;
71 :     return $self;
72 :     }
73 :    
74 :     # ----- utils -----
75 :    
76 :     sub readFile {
77 :     my $fileName = shift;
78 : sh002i 319 open INPUTFILE, "<", $fileName
79 :     or die "Couldn't open environment file $fileName: $!";
80 : malsyned 283 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