Problem with 3-arg open of pipe

  • Thread starter David Dyer-Bennet
  • Start date
D

David Dyer-Bennet

With Perl 5.6.1 on Linux.

The following statement is driving me crazy:

if (! open (FH, "|-", "qmail-inject", "-a", "dd-b\@dd-b.net")) {
die "failed to open";
}

I get the error:
Can't use an undefined value as filehandle reference
at ./t.pl line 13.

(test and production cases are being run with use strict and use
warnings).

qmail-inject is in the path. If I turn this into the two-arg form of
open, IT WORKS. (I don't want to do that in the production version
because of quoting/parsing issues with the args; that last email
address will actually be a list taken from an array).

I'm overlooking something obvious; I can smell it, but I can't find
what it *is*.

This should be a slam-dunk for somebody. What am I overlooking?
 
S

Steven Kuo

With Perl 5.6.1 on Linux.

The following statement is driving me crazy:

if (! open (FH, "|-", "qmail-inject", "-a", "dd-b\@dd-b.net")) {
die "failed to open";
}

I get the error:
Can't use an undefined value as filehandle reference
at ./t.pl line 13.

(test and production cases are being run with use strict and use
warnings).

(snipped)

I'm overlooking something obvious; I can smell it, but I can't find
what it *is*.

This should be a slam-dunk for somebody. What am I overlooking?




I believe you need version 5.8.0 (or one more recent) for the list
form of open for pipes. 'perldoc perlipc' concurs:


Since Perl 5.8.0, you can also use the list form of "open" for pipes :
the syntax

open KID_PS, "-|", "ps", "aux" or die $!;
 
D

David Dyer-Bennet

I believe you need version 5.8.0 (or one more recent) for the list
form of open for pipes. 'perldoc perlipc' concurs:


Since Perl 5.8.0, you can also use the list form of "open" for pipes :
the syntax

open KID_PS, "-|", "ps", "aux" or die $!;

Drat. I was being careful to look at 5.6 documentation, too, and I
did find the "-|" syntax and such. Oh well. Guess I'll have to join
that list and worry about quoting after all.

Thanks for the help!
 
S

Steven Kuo

Drat. I was being careful to look at 5.6 documentation, too, and I
did find the "-|" syntax and such. Oh well. Guess I'll have to join
that list and worry about quoting after all.

Thanks for the help!



If you're looking to avoid the shell, you can always fork and exec,
passing a LIST to the latter:

my $pid = open (FOO, '|-');

if ($pid) {
# parent (add SIGPIPE handler as needed)
print FOO "one\ntwo\nthree\n";
close FOO;
} else {
defined $pid or die "Could not fork : $!";

exec('/bin/cat', '-n')
or die "Can't exec program : $!";

# or in your case:
# exec ("qmail-inject", "-a", "dd-b\@dd-b.net") or die $!;

}

Is this what you meant by circumventing problems with "quoting"?
 
U

Uri Guttman

SK> If you're looking to avoid the shell, you can always fork and exec,
SK> passing a LIST to the latter:

SK> my $pid = open (FOO, '|-');

SK> if ($pid) {
SK> # parent (add SIGPIPE handler as needed)
SK> print FOO "one\ntwo\nthree\n";
SK> close FOO;
SK> } else {
SK> defined $pid or die "Could not fork : $!";

SK> exec('/bin/cat', '-n')
SK> or die "Can't exec program : $!";

SK> # or in your case:
SK> # exec ("qmail-inject", "-a", "dd-b\@dd-b.net") or die $!;

SK> }

read perlfork for a sub that does the same thing as that open. fine
workaround.

uri
 
D

David Dyer-Bennet

Steven Kuo said:
If you're looking to avoid the shell, you can always fork and exec,
passing a LIST to the latter: [snip]
Is this what you meant by circumventing problems with "quoting"?

Yes, that'd be safer. If I join the list together and pass it and let
the shell take it apart again, then it's kinda data-dependent whether
the results are the same as what I started with.

Now in this actual application, it's email addresses which I enter,
mostly my own, so I actually know it won't be a problem, until I
forget or share the code with someone else at some future date :).
So it's more a question of good form than actual necessity that I want
to be safe here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top