1: package Apache::run;
2: #
3: # $Id: run.pm,v 1.23 2002/02/15 21:33:12 albertel Exp $
4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: sub evaluateold {
29: my ($expression,$safeeval,$decls) = @_;
30: # print "inside2 evaluate $decls with $expression<br />\n";
31: # gerd's old method interpolates unset vars
32: # $safeeval->reval('return qq('.$expression.');');
33: unless (defined($expression)) { return ''; }
34: my $result = '';
35: $@='';
36: $safeeval->reval('$_=q|'.$expression.'|;');
37: if ($@ eq '') {
38: $safeeval->reval('{'.$decls.'$_=~s/(\$[A-Za-z]\w*)/(defined(eval($1))?eval($1):$1)/ge;}');
39: if ($@ eq '') {
40: $result = $safeeval->reval('return $_;');
41: } else {
42: &Apache::lonxml::error("substitution on:$expression:with:$decls:caused:$@:");
43: }
44: } else {
45: &Apache::lonxml::error("defining:$expression:caused:$@:");
46: }
47: return $result
48: }
49:
50: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
51: my %oldexpressions=();
52: while (!$oldexpressions{$_}) {
53: $oldexpressions{$_}=1;
54: $_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg;
55: }
56: ENDEVALUATE
57:
58: sub evaluate {
59: my ($expression,$safeeval,$decls) = @_;
60: unless (defined($expression)) { return ''; }
61: if (!$Apache::lonxml::evaluate) { return $expression; }
62: my $result = '';
63: $@='';
64: $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
65: "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
66: # $safeeval->reval('{'.$decls.';<< &evaluate(q|'.$expression.'|);}');
67: if ($@ eq '') {
68: $result = $safeeval->reval('return $_;');
69: chomp $result;
70: } else {
71: &Apache::lonxml::error("substitution on:$expression:with:$decls:caused $@");
72: }
73: return $result
74: }
75:
76: sub run {
77: my ($code,$safeeval,$hideerrors) = @_;
78: # print "inside run\n";
79: $@='';
80: my (@result)=$safeeval->reval($code);
81: if ($@ ne '' && !$hideerrors) {
82: &Apache::lonxml::error(":$@: occured while running :$code:");
83: }
84: if ( $#result < '1') {
85: return $result[0];
86: } else {
87: &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
88: return (@result);
89: }
90: }
91:
92: sub dump {
93: my ($target,$safeeval)=@_;
94: my $dump='';
95: foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
96: if (($symname!~/^\_/) && ($symname!~/\:$/)) {
97: if ($safeeval->reval('defined($'.$symname.')')) {
98: $dump.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
99: }
100: if ($safeeval->reval('defined(@'.$symname.')')) {
101: $dump.='@'.$symname.'=('.
102: $safeeval->reval('join(",",@'.$symname.')').")\n";
103: }
104: if ($safeeval->reval('defined(%'.$symname.')')) {
105: $dump.='%'.$symname.'=(';
106: $dump.=$safeeval->reval('join(",",map { $_."=>".$'.
107: $symname.'{$_} } sort keys %'.
108: $symname.')').")\n";
109: }
110: }
111: }
112: $dump.='';
113: return $dump;
114: }
115:
116: 1;
117: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>