In Perl, how can I check for Socket options without generating warnings?

I check the presence and default value of various socket options using Perl.

#!/usr/bin/perl -w use strict; use Socket; if (defined(SO_BROADCAST)) { print("SO_BROADCAST defined\n"); } if (defined(SO_REUSEPORT)) { print("SO_REUSEPORT defined\n"); } 

When I run this, it produces:

SO_BROADCAST defined

Your vendor has not defined Socket macro SO_REUSEPORT, used at ./checkopts.pl line 9

Is there a way to do this without generating exit alerts?

+4
source share
2 answers

Ask if sub is defined, but not if the value of the expression is defined:

 if (defined &SO_REUSEPORT) { ... } 

The documentation for defined explains:

You can also use defined(&func) to check if the &func routine has been defined. The return value is not changed by any forward &func declarations. Note that a subroutine that is not defined can still be called: its package may have an AUTOLOAD method, which makes it spring the first time it is called - see perlsub .

If sub is exported to your namespace, it must be defined.

+1
source

This message comes from AUTOLOAD in Socket.pm. When he finds a constant that is not supported, it croak s. You can catch this with eval :

  use Socket; if( defined eval { SO_REUSEPORT } ) { ...; } 
+9
source

Source: https://habr.com/ru/post/1300544/


All Articles