There is no good reason to use system echo
in Perl. You can simply open a pipe as a filehandle and print to it:
open my $mail, '|-', qw/mail -s error usr@mail.com/ or die $!;
print $mail $DBI::errstr, "\n";
EDIT: but to answer your question in more generality. Instead of relying on the shell to parse and unquote your command-line string and turn it into arguments, it's usually a better idea to use the explicit multi-argument form of system()
. So, instead of system "some_command --arg=$val"
and worrying whether $val
needs to be quoted for the shell, you can use system("some_command", "--arg=$val")
.