在Perl中,如何在不生成警告的情况下检查Socket选项的存在?
我正在使用Perl检查各种套接字选项的存在和默认值.
I'm checking for the existence and default values 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 outputs:
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 warnings in the output?
询问是否已定义子项,而不询问表达式的值是否已定义:
Ask whether the sub has been defined, not whether the expression's value is defined:
if (defined &SO_REUSEPORT) { ... }
defined
的文档说明:
The documentation for defined
explains:
您还可以使用
defined(&func)
来检查是否定义了子例程&func
.返回值不受&func
的任何前向声明的影响.请注意,未定义的子例程可能仍然可以调用:其程序包可能具有AUTOLOAD
方法,该方法使它在第一次被调用时就已存在-请参见
You may also use
defined(&func)
to check whether subroutine&func
has ever been defined. The return value is unaffected by any forward declarations of&func
. Note that a subroutine which is not defined may still be callable: its package may have anAUTOLOAD
method that makes it spring into existence the first time that it is called—see perlsub.
如果将子导出到您的命名空间,则必须对其进行定义.
If the sub is exported into your namespace, it has to be defined.