File:  [LON-CAPA] / loncom / xml / run.pm
Revision 1.25: download - view: text, annotated - select for diffs
Wed Apr 10 14:46:30 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Changes to make <script> errors print out cleaner.

    1: package Apache::run;
    2: #
    3: # $Id: run.pm,v 1.25 2002/04/10 14:46:30 matthew 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: $Apache::run::EVALUATE_STRING=<<'ENDEVALUATE';  
   29:   my %_LONCAPA_INTERNAL_oldexpressions=();
   30:   my $i=0;
   31:   while (!$_LONCAPA_INTERNAL_oldexpressions{$_}) {
   32:     $_LONCAPA_INTERNAL_oldexpressions{$_}=1;
   33:     $_ =~s/((?:\$|\&)(?:[\#|\$]*[A-Za-z][\w]*|\{[A-Za-z][\w]*\}))([\[\{][^\$\&\]\}]+[\]\}])*?(\([^\$\&\)]+\))*?(?=[^\[\{\(]|$)/eval(defined(eval($1.$2))?eval('$1.$2.$3'):'$1.$2.$3')/seg;
   34:     if ($i++ > 10 ) { last; }
   35:   }
   36: ENDEVALUATE
   37: 
   38: sub evaluate {
   39:   my ($expression,$safeeval,$decls) = @_;
   40:   unless (defined($expression)) { return ''; }
   41:   if (!$Apache::lonxml::evaluate) { return $expression; }
   42:   my $result = '';
   43:   $@='';
   44:   $safeeval->reval('{'.$decls.';$_=<<\'EXPRESSION\';'."\n".$expression.
   45: 		   "\n".'EXPRESSION'."\n".$EVALUATE_STRING.'}');
   46: #  $safeeval->reval('{'.$decls.';<< &evaluate(q|'.$expression.'|);}');
   47:   if ($@ eq '') {
   48:     $result = $safeeval->reval('return $_;');
   49:     chomp $result;
   50:   } else {
   51:     &Apache::lonxml::error("substitution on:$expression:with:$decls:caused $@");
   52:   }  
   53:   return $result
   54: }
   55: 
   56: sub run {
   57:   my ($code,$safeeval,$hideerrors) = @_;
   58: #  print "inside run\n";
   59:   $@='';
   60:   my (@result)=$safeeval->reval($code);
   61:   if ($@ ne '' && !$hideerrors) {
   62:     # Put in newline to make display of code easier to read.
   63:     &Apache::lonxml::error(":$@: occured while running :\n$code");
   64:   }
   65:   if ( $#result < '1') {
   66:     return $result[0];
   67:   } else {
   68:     &Apache::lonxml::debug("<b>Got lots results</b>:$#result:");
   69:     return (@result);
   70:   }
   71: }
   72: 
   73: sub dump {
   74:   my ($target,$safeeval)=@_;
   75:   my $dump='';
   76:   foreach my $symname (sort keys %{$safeeval->varglob('main::')}) {
   77:     if (($symname!~/^\_/) && ($symname!~/\:$/)) {
   78:       if ($safeeval->reval('defined($'.$symname.')')) {
   79: 	$dump.='$'.$symname.'='.$safeeval->reval('$'.$symname)."\n";
   80:       }	
   81:       if ($safeeval->reval('defined(@'.$symname.')')) {
   82: 	$dump.='@'.$symname.'=('.
   83: 	  $safeeval->reval('join(",",@'.$symname.')').")\n";
   84:       }
   85:       if ($safeeval->reval('defined(%'.$symname.')')) {
   86: 	$dump.='%'.$symname.'=(';
   87: 	$dump.=$safeeval->reval('join(",",map { $_."=>".$'.
   88: 				$symname.'{$_} } sort keys %'.
   89: 				$symname.')').")\n";
   90:       }
   91:     }
   92:   }
   93:   $dump.='';
   94:   return $dump;
   95: }
   96: 
   97: 1;
   98: __END__;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>