Subject: | "open $fh,'<',$name || die" gets past InputOutput::RequireCheckedOpen |
Consider this program:
#!/usr/bin/perl
use warnings;
use strict;
my $name = 'nonexistent-filename.txt';
open my $fh_one, '<', $name;
close $fh_one;
open my $fh_two, '<', $name || die;
close $fh_two;
The first open will get flagged by InputOutput::RequireCheckedOpen. The
second open will NOT get flagged, even though the return value is not
checked. Because of the order of operations, and "||" binding tighter
than "or", the second open is the same as
open my $fh_two, '<', ($name || die);
This is a common problem with people interchanging "||" for "or" and not
realizing the impact.
Running code through B::Deparse makes this clear, too.
$ cat or2
open $fh, '<', $name || die;
open $fh, '<', $name or die;
open $fh, '<', ($name || die);
open $fh, '<', ($name or die);
(open $fh, '<', $name) || die;
(open $fh, '<', $name) or die;
$ perl -MO=Deparse or2
open $fh, '<', $name || die;
die unless open $fh, '<', $name;
open $fh, '<', $name || die;
open $fh, '<', $name || die;
die unless open $fh, '<', $name;
die unless open $fh, '<', $name;
or2 syntax OK