This is a continuation of my previous question:
In Perl, how can I check for the existence of Socket options without generating warnings?
If I run the following code I get the result I expect:
#!/usr/bin/perl -w
use strict;
use diagnostics;
use Socket qw(:all);
my %opts;
if ( defined( eval { SO_REUSEPORT } ) ) {
$opts{'SO_REUSEPORT'}
= {opt_level =>SOL_SOCKET,opt_name=>SO_REUSEPORT,opt_print=>\&sock_str_flag};
} else {
print "SO_REUSEPORT undefined\n";
$opts{'SO_REUSEPORT'}
= {opt_level =>0,opt_name=>0,opt_print=>undef};
}
=head
# IPV6 options
if ( defined( eval { IPV6_DONTFRAG } ) ) {
$opts{'IPV6_DONTFRAG'}
= {opt_level =>IPPROTO_IPV6,opt_name=>IPV6_DONTFRAG,opt_print=>\&sock_str_flag};
} else {
print "IPV6_DONTFRAG undefined\n";
$opts{'IPV6_DONTFRAG'}
= {opt_level =>0,opt_name=>0,opt_print=>undef};
}
=cut
It outputs:
anon@perl$ ./test.pl
SO_REUSEPORT undefined
But if I uncomment the block for IPV6_DONTFRAG
I get:
Bareword "IPV6_DONTFRAG" not allowed while "strict subs" in use at ./test.pl line 17.
Bareword "IPV6_DONTFRAG" not allowed while "strict subs" in use at ./test.pl line 17.
Why is one undefined bareword causing it to barf and the other not? And how can the error be propagating out of the eval { }
block?
Edit
Apparently, SO_REUSEPORT
is exported by Socket.pm in some manner as it's in the @EXPORT array. So apparently it's defined but using it throws an error which the eval catches.
That still doesn't explain what's going on with IPV6_DONTFRAG
. I suppose I would need to define it myself and then just call getsockopt
to check if it's supported...