Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to evaluate a mathematical formula that is stored in another file?

by hippo (Bishop)
on Mar 20, 2018 at 11:21 UTC ( [id://1211302]=note: print w/replies, xml ) Need Help??


in reply to How to evaluate a mathematical formula that is stored in another file?

Here's an SSCCE:

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; my $x = 7; my $y = 5; my $z; my $formula = '$z = $x + $y'; eval $formula; is ($z, 12)

Note that your script would be essentially executing arbitrary perl code from another source so this is a big security risk and you ought not to do it.

  • Comment on Re: How to evaluate a mathematical formula that is stored in another file?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to evaluate a mathematical formula that is stored in another file?
by skooma (Novice) on Mar 20, 2018 at 11:34 UTC
    Thanks. What if I wanted to execute a basic mathematical formula? That is, not a Perl code from another source file but just a mathematical expression, #start of formula file z = a + b #end of formula file a and b values will be taken from my data/parameter file. This is what I am going for. Sorry if my explanation is not clear.

      If the expressions aren't Perl then you cannot eval them. Sounds like you want Math::Symbolic instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found