#!/usr/bin/perl print diff(shift)."\n"; sub diff { my $_ = shift; # I make this local since I'll be making # recursive calls # I'll just include addition: return diff($1)."+".diff($2) if m/(.+?)\+(.+); ... # other differentiation rules here }