'Cannot use undefined as a file descriptor reference in Perl

I am getting this error when I run my perl script:

Unable to use undefined as a file descriptor reference on line 44.

Line 44:

open my $fh, "|-", "mail", "-s", $subject, $owner, "-c", $sendto
    or die "$0: could not start mail: $!";

      

Everything looks good. What does the error mean?

+2


source to share


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


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 simple open

    .
+1


source







All Articles