在Perl中,如何在不生成警告的情况下检查Socket选项的存在? [英] In Perl, how can I check for the existence of Socket options without generating warnings?

查看:62
本文介绍了在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 an AUTOLOAD 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.

这篇关于在Perl中,如何在不生成警告的情况下检查Socket选项的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆