Most likely you are not using strict. Your line makes little sense because open usually takes at least two parameters, the filehandle to be opened and the name of the resource to be accessed:
open(my $fh, "<", $file) or die("file open failed for '$file': $!");
That said, your code fails for me as expected:
X:\>perl -e "open($file) || die('file open failed');"
file open failed at -e line 1.
X:\>perl -e "open('xx') || die('file open failed');"
file open failed at -e line 1.
I didn't know and haven't used the 1-argument form of open and for good reason, because that means action at a very large distance:
perl -e "$FILE='file_that_exists';open(FILE) || die('file open failed'
+);"
In this case, the 1-argument form does not die because the file named in $FILE exists. But that is substantially different from your code.
Update You silently update your node. I consider that quite rude. Please mark updates to your node with <b>Update:</b> or something and don't overwrite the complete node.
Your new code fails for me as expected:
X:\>perl -w tmp.pl
Name "main::IN" used only once: possible typo at tmp.pl line 5.
Couldn't open `/tmp/test123' at tmp.pl line 5.
X:\>type tmp.pl
use strict;
sub push_report {
my $idx = shift;
my $file= "/tmp/test123";
open(IN, $file) || die "Couldn't open `$file'";
}
Maybe you want to do more rigorous checks:
use strict;
sub push_report {
my $idx = shift;
my $file= "/tmp/test123";
if (-e $file) {
warn "'$file' exists";
} else {
warn "'$file' does not exist";
};
if (-f $file) {
warn "'$file' exists as a file";
};
if (-d $file) {
warn "'$file' exists as a directory";
};
open(IN, $file) || die "Couldn't open `$file'";
}
|