2

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

Can't use an undefined value as filehandle reference at scr line 44.

Line 44:

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

Everything looks okay. What does the error mean?

3
  • 1
    Do you have an ancient version of perl, such as 5.005 or so, installed? Commented Nov 17, 2010 at 7:42
  • 3
    Why are you working with a nine year old version of Perl? Update your Perl, your sysadmin or your job :-) Commented Nov 17, 2010 at 12:07
  • blog.urth.org/2010/11/… Commented Nov 17, 2010 at 17:02

2 Answers 2

4

I think the error message is in error. This code:

open my $fh, '|-', 'perl', '-v' or die "$!\n";

dies with the error message

Can't use an undefined value as filehandle reference...

under Perl 5.6.1, but dies with

List form of pipe open not implemented...

under Perl 5.12.1 (on Win32).

"List form" means specifying the program and its arguments as separate values. The single-value form runs on both versions:

open my $fh, '|-', 'perl -v' or die "$!\n";
Sign up to request clarification or add additional context in comments.

Comments

1

That's a pretty old perl, I'm guessing that that version doesn't support lexical file handles so "open my $fh" won't work as ancient perl will see that as, essentially, open undef and hence the error message. Possible solutions:

  • Upgrade your perl out of the bronze age.
  • Use the old school open FH syntax instead.
  • Use gensym to generate a symbol that you can use as a file handle.
  • Use one of the IO::* classes of plain open.

2 Comments

It supports lexical filehandles: my $fh = "STDIN" is of course a lexical filehandle; so is my $fh = do{local *FH}. It simply doesn’t support the autovivification of lvaluable arguments previously undefined into references to typeglobs blessed into the IO::Handle package with a destructor to close and hide any failure on said handle — which is a different thing.
So I'm thinking around the right area then: upgrade perl or downgrade the code. Been awhile since I've dealt with 5.6 and I switched to IO::Handle as soon I found out about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.