my $cmd = "mail ..."; open(my $fh, '|-', $cmd) or ...; print $fh $msg; close($fh);