I use the AnyEvent::DNS module.
I want to disable IPv6, so that the resolver only makes a request for A record.
AnyEvent::DNS, uses the environment variable $ENV{PERL_ANYEVENT_PROTOCOLS}
But setting the variable does not work; the resolver still sends two requests A and AAAA
Code from AnyEvent::DNS:
our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
BEGIN {
...;
my $idx;
$PROTOCOL{$_} = ++$idx
for reverse split /\s*,\s*/,
$ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
}
How to define an environment variable before loading modules?
Since the code that checks the environment variable is in a
BEGINblock, it will be run immediately once the Perl compiler reaches it.When Perl starts compiling your script, it checks for
usestatements first. So when youuse AnyEvent::DNS, Perl loads that module and parses the file.BEGINblocks are executed at that stage, while code in methods will only be compiled, not executed.So if you have something like the following, the code you showed above will be run before you even set that variable.
There are two ways you can circumvent that.
You can put the assignment in your own
BEGINblock before you load AnyEvent::DNS. That way it will be set first.Alternatively, you can just call your program with the environment variable set for it from the shell.
The second one is more portable, in case you later want it to do IPv6 after all.
Read more about
BEGINin perlmod.