Annotation of loncom/xml/run.pm, revision 1.23
1.2 albertel 1: package Apache::run;
1.21 www 2: #
1.23 ! albertel 3: # $Id: run.pm,v 1.22 2001/12/11 15:48:51 albertel Exp $
1.21 www 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: #
1.1 sakharuk 27:
1.10 albertel 28: sub evaluateold {
1.4 albertel 29: my ($expression,$safeeval,$decls) = @_;
1.15 albertel 30: # print "inside2 evaluate $decls with $expression<br />\n";
1.3 albertel 31: # gerd's old method interpolates unset vars
1.5 albertel 32: # $safeeval->reval('return qq('.$expression.');');
1.22 albertel 33: unless (defined($expression)) { return ''; }
1.7 albertel 34: my $result = '';
35: $@='';
1.9 sakharuk 36: $safeeval->reval('$_=q|'.$expression.'|;');
1.7 albertel 37: if ($@ eq '') {
1.9 sakharuk 38: $safeeval->reval('{'.$decls.'$_=~s/(\$[A-Za-z]\w*)/(defined(eval($1))?eval($1):$1)/ge;}');
1.7 albertel 39: if ($@ eq '') {
40: $result = $safeeval->reval('return $_;');
41: } else {
1.23 ! albertel 42: &Apache::lonxml::error("substitution on:$expression:with:$decls:caused:$@:");
1.7 albertel 43: }
44: } else {
1.23 ! albertel 45: &Apache::lonxml::error("defining:$expression:caused:$@:");
1.7 albertel 46: }
1.10 albertel 47: return $result
48: }
49:
1.11 albertel 50: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';
51: my %oldexpressions=();
52: while (!$oldexpressions{$_}) {
53: $oldexpressions{$_}=1;
1.13 www 54: $_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg;
1.11 albertel 55: }
56: ENDEVALUATE
57:
1.10 albertel 58: sub evaluate {
59: my ($expression,$safeeval,$decls) = @_;
1.22 albertel 60: unless (defined($expression)) { return ''; }
1.16 albertel 61: if (!$Apache::lonxml::evaluate) { return $expression; }
1.10 albertel 62: my $result = '';
63: $@='';
1.11 albertel 64: $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
65: "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
66: # $safeeval->reval('{'.$decls.';<< &evaluate(q|'.$expression.'|);}');
1.10 albertel 67: if ($@ eq '') {
68: $result = $safeeval->reval('return $_;');
1.13 www 69: chomp $result;
1.10 albertel 70: } else {
71: &Apache::lonxml::error("substitution on:$expression:with:$decls:caused $@");
72: }
1.7 albertel 73: return $result
1.2 albertel 74: }
75:
76: sub run {
1.18 albertel 77: my ($code,$safeeval,$hideerrors) = @_;
1.3 albertel 78: # print "inside run\n";
1.7 albertel 79: $@='';
1.14 albertel 80: my (@result)=$safeeval->reval($code);
1.18 albertel 81: if ($@ ne '' && !$hideerrors) {
1.23 ! albertel 82: &Apache::lonxml::error(":$@: occured while running :$code:");
1.7 albertel 83: }
1.14 albertel 84: if ( $#result < '1') {
85: return $result[0];
86: } else {
87: &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
88: return (@result);
89: }
1.2 albertel 90: }
91:
1.19 albertel 92: sub dump {
93: my ($target,$safeeval)=@_;
94: my $dump='';
95: foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
96: if (($symname!~/^\_/) && ($symname!~/\:$/)) {
1.20 albertel 97: if ($safeeval->reval('defined($'.$symname.')')) {
1.19 albertel 98: $dump.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
99: }
1.22 albertel 100: if ($safeeval->reval('defined(@'.$symname.')')) {
1.19 albertel 101: $dump.='@'.$symname.'=('.
102: $safeeval->reval('join(",",@'.$symname.')').")\n";
103: }
1.22 albertel 104: if ($safeeval->reval('defined(%'.$symname.')')) {
1.19 albertel 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:
1.2 albertel 116: 1;
117: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>