The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Math::Symbolic::Custom::Contains - Find subtrees in Math::Symbolic expressions


  use Math::Symbolic qw/:all/;
  use Math::Symbolic::Custom::Contains;
  my $formula = parse_from_string('a*b+c');

  print 'found product' if $formula->contains_operator(B_PRODUCT);
  # works
  print 'found division' if $formula->contains_operator(B_DIVISION);


This module extends the functionality of Math::Symbolic by offering facilities to test a Math::Symbolic tree for existance of a specific subtree in the Math::Symbolic tree.

As of version 0.10, this has only been implemented for operators via the contains_operator method.

The module adds methods to all Math::Symbolic objects.

$ms_tree->contains_operator( [Operator type] )

This method does not modify the Math::Symbolic tree itself, but instead tests the tree for existance of an operator of the specified operator type. It returns undef if no such operator exists in the tree and returns a reference to the first occurrance of the operator if there are such operators.

Operator types are constants exported by Math::Symbolic. Please refer to the Math::Symbolic manual for details.

If the operator type is omitted, the match will be performed for any operator. That means if the tree contains any operators at all, a reference to the top-most operator will be returned. (Which will always be the top-most node of the tree anyway.)


Please send feedback, bug reports, and support requests to one of the contributors or the Math::Symbolic mailing list.

List of contributors:

  Steffen M�ller, symbolic-module at steffen-mueller dot net


New versions of this module can be found on or CPAN.


Math::Symbolic::Custom, Math::Symbolic::Custom::Base, Math::Symbolic::Custom::DefaultTests