[system] / trunk / pg / lib / Parser / String.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/String.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5509 - (download) (as text) (annotate)
Sat Sep 15 00:56:51 2007 UTC (12 years, 3 months ago) by dpvc
File size: 2615 byte(s)
Formula objects and Context objects contain reference loops, which
prevent them from being freed properly by perl when they are no longer
needed.  This is a source of an important memory leak in WeBWorK.  The
problem has been fixed by using Scalar::Util::weaken for these
recursive references, so these objects can be freed properly when they
go out of scope.  This should cause an improvement in the memory usage
of the httpd child processes.

    1 #########################################################################
    2 #
    3 #  Implements constant string values
    4 #    (Used for things like INFINITY, and so on)
    5 #
    6 package Parser::String;
    7 use strict;
    8 our @ISA = qw(Parser::Item);
    9 
   10 $Parser::class->{String} = 'Parser::String';
   11 
   12 #
   13 #  Mark the created word as infinity or negative infinity, and so on.
   14 #
   15 sub new {
   16   my $self = shift; my $class = ref($self) || $self;
   17   my $equation = shift; my $strings = $equation->{context}{strings};
   18   my ($value, $ref) = @_;
   19   my $def = $strings->{$value};
   20   unless ($def) {
   21     $def = $strings->{uc($value)};
   22     $def = {} if $def->{caseSensitive} && $value ne uc($value);
   23   }
   24   $value = $def->{alias}, $def = $strings->{$value} while defined($def->{alias});
   25   my $str = bless {
   26     value => $value, type => $Value::Type{string}, isConstant => 1,
   27     def => $def, ref => $ref, equation => $equation,
   28   }, $class;
   29   $str->weaken;
   30   $str->{isInfinite} = 1 if ($def->{infinite});
   31   $str->{isInfinity} = 1 if ($def->{infinite} && !$def->{negative});
   32   $str->{isNegativeInfinity} = 1 if ($def->{infinite} && $def->{negative});
   33   return $str;
   34 }
   35 
   36 sub newInfinity {
   37   my $self = shift; my $equation = shift; my $value = shift;
   38   my $neg = ($value =~ s/^-//);
   39   $self = $self->new($equation,$value,@_);
   40   if ($neg) {$self->{isInfinity} = 0; $self->{isNegativeInfinity} = 1}
   41   return $self;
   42 }
   43 
   44 #
   45 #  Make a Value::String or Value::Infinity object
   46 #
   47 sub eval {
   48   my $self = shift; my $context = $self->context;
   49   return $self->Package("String")->make($context,$self->{value}) unless $self->{isInfinite};
   50   my $I = $self->Package("Infinity")->new($context);
   51   $I = $I->neg if $self->{isNegativeInfinity};
   52   return $I;
   53 }
   54 
   55 #
   56 #  Return the replacement string if there is one,
   57 #    or let Value handle it if we can, otherwise return the string
   58 #
   59 sub string {
   60   my $self = shift;
   61   return $self->{def}{string} if defined($self->{def}{string});
   62   my $value = $self->eval;
   63   return $value unless Value::isValue($value);
   64   return $value->string($self->{equation});
   65 }
   66 
   67 #
   68 #  Typeset the value in \rm
   69 #
   70 sub TeX {
   71   my $self = shift;
   72   return $self->{def}{TeX} if defined($self->{def}{TeX});
   73   my $value = $self->eval; $value =~ s/([ _])/\\$1/g;
   74   return '{\rm '.$value.'}' unless Value::isValue($value);
   75   return $value->TeX($self->{equation});
   76 }
   77 
   78 #
   79 #  Put the value in quotes
   80 #
   81 sub perl {
   82   my $self = shift;
   83   return $self->{def}{perl} if defined($self->{def}{perl});
   84   my $value = $self->eval;
   85   return "'".$value."'" unless Value::isValue($value);
   86   return $value->perl;
   87 }
   88 
   89 #########################################################################
   90 
   91 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9