You have a typo in the code you submitted (second open argument), but this does not explain the error message. The message for this problem will be as follows:
Unknown open() mode ',' at ...
Your problem is related to priority. || binds too tightly, forcing Perl to process this entire expression as the third argument to open:
$file || croak $!
As a result, despite the fact that open does not work (probably because $file not a valid file name), croak fails (because $file is true and || short-circuit). After open fails, your program tries to read some lines from an unopened file descriptor, and you get this error message:
readline() on closed filehandle $fh at ...
Instead, you want to use one of the following. The second option works (unlike your code), because or has a low priority.
open(my $fh, '<', $file) || croak ...; open my $fh, '<', $file or croak ...;
See perlop for more information on operator precedence . The relevant point in your case is that the || has a higher priority than the list separator (comma).
source share