| Refresh | Home EGTry.com

replace with call back function


use strict;

my $text="the number is 3+4";
print "before: $text\n";

$text =~ s/(\d+)\+(\d+)/&hello($&, $1, $2)/e;

print "after: $text\n";


sub hello {
 my ($line, $n1, $n2)=@_;
 my $sum=$n1+$n2;
 return "$line=$sum";
}




output

before: the number is 3+4
after: the number is 3+4=7