'Cannot use undefined as a file descriptor reference in Perl
2 answers
I think the error message is wrong. This code:
open my $fh, '|-', 'perl', '-v' or die "$!\n";
dies with error message
Can't use undefined as a file descriptor reference ...
under Perl 5.6.1, but dies with
The list of pipe opening forms is not implemented ...
under Perl 5.12.1 (on Win32).
"List form" means specifying the program and its arguments as separate values. The unambiguous form is performed in both versions:
open my $fh, '|-', 'perl -v' or die "$!\n";
+3
source to share
This is quite old perl, I assume this version does not support lexical file descriptors, so "open my $ fh" will not work as ancient perl would see it as essentially open undef
and therefore an error message. Possible solutions:
- Update your perl from the Bronze Age.
- Use old school syntax instead
open FH
. - Use
gensym
to create a symbol that you can use as a file descriptor. - Use one of the classes
IO::*
for simpleopen
.
+1
source to share