Subject: | [PATCH] nslookup Fork Fails on Windows |
Hello there. Thank you very much for writing Email::Valid -- it's an excellent module, and we make good use of it. Recently we've tried deploying it on Windows and found a problem with the code that uses the external nslookup command for the MX check. This includes the line:
if (my $fh = new IO::File '-|') {
Unfortunately that doesn't work on Windows. It's documented not to work in perlfork (search for "Forking pipe") and it gives this error message:
'-' is not recognized as an internal or external command, operable program or batch file.
perlfork suggests a work-around, and provides the function pipe_from_fork. However Uri Guttman reports that it doesn't work:
http://groups.google.com/groups?threadm=x73cbvohjn.fsf@mail.sysarch.com
And it doesn't seem to work for me either.
I tried changing Email::Valid to use IO::Pipe instead, but that provides no way of either capturing or squashing things sent to standard error, and nslookup prints to stderr if a domain isn't found. So I've used IO::CaptureOutput instead. This seems to work (I've tested it on both Windows and FreeBSD). Please see the attached patch.
Let me know if you have any questions.
Cheers.
Smylers
--- /usr/local/lib/perl5/site_perl/5.8.5/Email/Valid.pm Sat Aug 23 09:28:03 2003
+++ ./Valid.pm Wed Mar 16 13:57:45 2005
@@ -5,7 +5,7 @@
@NSLOOKUP_PATHS $Details $Resolver $Nslookup_Path
$DNS_Method $TLD $Debug );
use Carp;
-use IO::File;
+use IO::CaptureOutput qw<capture_exec>;
use Mail::Address;
use File::Spec;
@@ -142,21 +142,11 @@
return 1 if gethostbyname $host;
# Check for an MX record
- if (my $fh = new IO::File '-|') {
- my $response = <$fh>;
- print STDERR $response if $Debug;
- close $fh;
- $response =~ /$NSLOOKUP_PAT/io or return $self->details('mx');
- return 1;
- } else {
- open OLDERR, '>&STDERR' or croak "cannot dup stderr: $!";
- open STDERR, '>&STDOUT' or croak "cannot redirect stderr to stdout: $!";
- {
- exec $Nslookup_Path, '-query=mx', $host;
- }
- open STDERR, ">&OLDERR";
- croak "unable to execute nslookup '$Nslookup_Path': $!";
- }
+ my $response = capture_exec $Nslookup_Path, '-query=mx', $host;
+ croak "unable to execute nslookup '$Nslookup_Path': exit $?" if $?;
+ print STDERR $response if $Debug;
+ $response =~ /$NSLOOKUP_PAT/io or return $self->details('mx');
+ return 1;
}
# Purpose: Check whether a top level domain is valid for a domain.