http://www.perlmonks.org?node_id=906466

muxxum has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I was wondering if anyone can explain the difference between the outputs of two following programs. The only difference between the programs is that 0 is assigned to $! in the second program.

Program 1:

stat("non_existing_file"); die "died at first stat" if -$!; stat("non_existing_file"); die "died at second stat" if -$!;
Output:
died at first stat at a.pl line 3.

Program 2:

$! = 0; stat("non_existing_file"); die "died at first stat" if -$!; stat("non_existing_file"); die "died at second stat" if -$!;

Output:

died at second stat at a.pl line 5.

If 0 is assigned to $! before the first stat, the first failed stat does not set the $!.

I also noticed that if instead of $!=0 we do a undef $!, the program behaves as the first one.

Thanks!