Search code examples
perl

What does if (print) means in Perl?


I got the following code

if (print msgsnd($ipc_msg_to_mppt_id, $msg, 0)) {

what is the purpose of print here? What is returns?

Documentation says it returns true if successful. But how can printing be unsuccessfull?


Solution

  • Printing isn't necessary as simple as dumping the output to a console. It could also be redirected to a file or some other kind of pipe. If it's redirected to a file you don't have write access to, then printing to that file will fail. If it's piped into another program and the latter program terminates, then writing to it will cause a broken pipe error.

    As a general principle, I/O operations are outside the control of your program, so you should always assume that they can fail. Reading to or writing from a file, the console, or any kind of socket or pipe, can always, without warning, fail. So if you want your program to do something about it, you'll need to check the return value of functions like print.