One issue that I didn't see touched on was execution speed. It appears that you'd want to calculate, for example, (X/2)^4 for quite a few values of X. The fastest way to do this would be to create a subroutine that does the calculation. For example, a closure:

my $sub= eval "sub { my($x)=@_; return ($x/2)**4 }"; die $@ if $@; for my $x ( 0..100 ) { $x /= 10; my $y= $sub->( $x ); plot( $x, $y ); }
So I'd probably validate the input and use eval to build an anonymous subroutine similar to what I show above.

So the trick is deciding what to allow that will be safe.

If you only allow digits and mathish punctuation, then I can't come up with a way that the result would be dangerous:

if( $equ =~ m#[^-\s\d+*/()]# ) { # refuse to use it
You could even allow a specific list of variable names and functions:
my $equ= $q->param("equation"); my( $var, $expr )= $equ =~ /^\s*(\w+)\s*=(.*)/ or fail("Invalid equation"); my @words= qw( x y z abs atan2 cos exp log sin sqrt ); my %words; @words{@words}= (" ") x @words; ( my $check= $expr ) =~ s/(\w+)/ $words{$1} ? "" : "x" /ge; if( $check =~ m#([^-\s\d+*/()]+)# ) { fail( "Disallowed function/variable ($1)" ); } @words{qw(x y z)}= qw( $x $y $z ); $expr =~ s/(\w+)/ $words{$1} /ge; my $sub= eval 'sub { my($x,$y,$z)= @_; return ' . $expr . '}'; fail( "Invalid expression ($expr): $@" ) if $@;

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Parsing Math Strings by tye
in thread Parsing Math Strings by pokemonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.