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