Question

The following is the code that I tried to execute

$a=0;  
if($a==0){die  print"zero"};  

and I get output as follows

1 at test.pl line 2  
Zero  

I am wondering what is 1 that is printed first, is it something like die is printing the value returned by the if statement? Any help would be appriciated

Was it helpful?

Solution

In

die print "zero";

die will use the return value of print "zero";, which is 1, as its argument. So you see the 1 at ... message.

By default, the STDOUT of interactive program is line buffered, but STDERR is unbuffered, that is why you see the 1 at ... (on stderr) first, and zero (on stdout) later. If you change that print "zero" to print "zero\n", you will see zero comes first.

OTHER TIPS

Actually, print function returned a 1 which die promptly inserted into its error message.

Control never returns to the if stmt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top