in reply to Perl and maths

Why not just break up the mathematical expression into in a tree? Then you can "percolate up" the tree generating the new expression line by line. The first line printed by perl would be all the leaf nodes with their conecting operator signs (+-*/).

The second line would be the leaf node operations completed and so on until you only have a root

You would basically need 2 routines.
1. Build-Tree -> responsible for taking an expression and making a tree out of it

2. iterate-tree -> responsible for performing the mathematical operations described at leaf level and replacing the parent node with the new answer. i.e.

         <some-root>  <some-root>
             /             /
            +      =>     9
          /   \
         5     4
 

Printing intermediate results is a breeze since it is in tree form. Just make sure that you adhere to the same principle you used when generating the tree (in-order, pre-order, or post-order).