use strict; my $text=<<'_end'; 1 + 1 2+2 and 3+3 _end print "before:\n$text\n"; $text =~ s/(\d+)\s*\+\s*(\d+)/&hello($&, $1, $2)/ge; print "after:\n$text\n"; sub hello { my ($line, $n1, $n2)=@_; my $sum=$n1+$n2; return "$line=$sum"; }
before: 1 + 1 2+2 and 3+3 after: 1 + 1=2 2+2=4 and 3+3=6