# Call fraction and perform the conditional test there $str = fraction($str); ### subroutine fraction sub fraction { my $str = shift; if ($str =~ /\d+_\d+) { # convert $str to fraction using mimetex syntax } return $str; }