Value::isValue($f)will return true if
$f
is a MathObject and false otherwise. There is also one for telling is something is a Formula object (as opposed to other MathObjects:
Value::isFormula($f)So you could use something like
sub mySubroutine { my $f = shift; if (Value::isFormula($f)) {$f = $f->perlFunction} ... }to convert a MathObject Formula into a subroutine reference. That way you could pass either a MathObject or a CODE reference to
mySubroutine
and $f
would always end up being a CODE reference. You could even do
sub mySubroutine { my $f = shift; if (ref($f) ne "CODE") { $f = Value->Package("Formula")->new($f)->perlFunction; } ... }so that if $f is passed as a string or as a Formula or as some constant, it will be turned into a formula and converted to a CODE reference. This approach does not require that MathObjects.pl be loaded (which using
main::Formula()
in place of Value->Package("Formula")->new()
would).
The problem with using ref($f)
and looking for Value::Formula
is that there are a number of MathObjects that make subclasses of Value::Formula
, so unless you check for those as well, you might not handle all the formulas that people would pass to you. And since new subclasses can be made at any time, you need a better way (like Value::isFormula()
).
Hope that helps.
Davide