What's wrong with using backticks in a void context?

Strictly speaking, nothing. Stylistically speaking, it's not a good way to write maintainable code. Perl has several operators for running external commands. Backticks are one; they collect the output from the command for use in your program. The system function is another; it doesn't do this.

Writing backticks in your program sends a clear message to the readers of your code that you wanted to collect the output of the command. Why send a clear message that isn't true?

Consider this line:

    `cat /etc/termcap`;
You forgot to check $? to see whether the program even ran correctly. Even if you wrote

    print `cat /etc/termcap`;
this code could and probably should be written as

    system("cat /etc/termcap") == 0
	or die "cat program failed!";
which will get the output quickly (as it is generated, instead of only at the end) and also check the return value.

system() also provides direct control over whether shell wildcard processing may take place, whereas backticks do not.


Back to perlfaq8