[system] / trunk / webwork-modperl / lib / WeBWorK / CourseEnvironment.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork-modperl/lib/WeBWorK/CourseEnvironment.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 412 - (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 : sh002i 412 use WeBWorK::Utils qw(readFile);
7 : malsyned 283
8 : sh002i 319 # 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 : malsyned 283 sub new {
13 : sh002i 319 my $invocant = shift;
14 :     my $class = ref($invocant) || $invocant;
15 : malsyned 283 my $webworkRoot = shift;
16 :     my $courseName = shift;
17 : sh002i 319 my $safe = Safe->new;
18 : malsyned 283
19 : sh002i 319 # set up some defaults that the environment files will need
20 :     $safe->reval("\$webworkRoot = '$webworkRoot'");
21 :     $safe->reval("\$courseName = '$courseName'");
22 :    
23 : malsyned 283 # determine location of globalEnvironmentFile
24 :     my $globalEnvironmentFile = "$webworkRoot/conf/global.conf";
25 : sh002i 319
26 : malsyned 283 # read and evaluate the global environment file
27 :     my $globalFileContents = readFile($globalEnvironmentFile);
28 : sh002i 319 $safe->reval($globalFileContents);
29 :    
30 :     # if that evaluation failed, we can't really go on...
31 :     # we need a global environment!
32 : malsyned 283 $@ and die "Could not evaluate global environment file $globalEnvironmentFile: $@";
33 :    
34 :     # determine location of courseEnvironmentFile
35 : sh002i 319 # 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 : malsyned 283 # read and evaluate the course environment file
42 : sh002i 319 # if readFile failed, we don't bother trying to reval
43 :     my $courseFileContents = eval { readFile($courseEnvironmentFile) }; # catch exceptions
44 :     $@ or $safe->reval($courseFileContents);
45 : malsyned 304
46 : sh002i 319 # 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 : malsyned 283 bless $self, $class;
72 :     return $self;
73 :     }
74 :    
75 : sh002i 324 sub hash2string {
76 :     my $hr = shift;
77 :     my $indent = shift || 0;
78 :     my $result;
79 :     foreach (keys %$hr) {
80 :     $result .= "\t"x$indent . "{$_} =";
81 :     if (ref $hr->{$_} eq 'HASH') {
82 :     $result .= "\n";
83 :     $result .= hash2string($hr->{$_}, $indent+1);
84 :     } elsif (ref $hr->{$_} eq 'ARRAY') {
85 :     $result .= "\n";
86 :     $result .= array2string($hr->{$_}, $indent+1);
87 :     } else {
88 :     $result .= " " . $hr->{$_} . "\n";
89 :     }
90 :     }
91 :     return $result;
92 :     }
93 :    
94 :     sub array2string {
95 :     my $ar = shift;
96 :     my $indent = shift || 0;
97 :     my $result;
98 :     foreach (0 .. @$ar-1) {
99 :     $result .= "\t"x$indent . "[$_] =";
100 :     if (ref $ar->[$_] eq 'HASH') {
101 :     $result .= "\n";
102 :     $result .= hash2string($ar->[$_], $indent+1);
103 :     } elsif (ref $ar->[$_] eq 'ARRAY') {
104 :     $result .= "\n";
105 :     $result .= array2string($ar->[$_], $indent+1);
106 :     } else {
107 :     $result .= " " . $ar->[$_] . "\n";
108 :     }
109 :     }
110 :     return $result;
111 :     }
112 :    
113 : malsyned 283 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9