$str = "()"; if( $str =~ s//(?{ &getString($1) })<\/ref>/ ){ print "Match was found." . "\n"; } print $str . "\n"; sub getString($s){ my $s = shift; ...more processing of $s... return $s; }