如何为 Perl 变量设置默认值?
我对 Perl 完全陌生。我需要使用外部模块 HTTP::BrowserDetect。我正在测试一些代码并尝试从 os_string 方法获取操作系统的名称。因此,我只是初始化了该对象并创建了一个变量来存储返回的值。
my $ua = HTTP::BrowserDetect->new($user_agent);
my $os_name = $ua->os_string();
print "$user_agent $os_name\n";
有一些用户代理不是浏览器用户代理,因此它们不会从 os_string 获取任何值。我收到错误 Use of uninitialized value $os_name in concatenation (.) or string
当 $os_name 未初始化,因为方法 os_string 返回 undef 时,我该如何处理这种情况(这是我的想法通过阅读模块源代码发生)。我想应该有一种方法来给出默认字符串,例如在这些情况下没有操作系统。
I am completely new to Perl. I needed to use an external module HTTP::BrowserDetect. I was testing some code and tried to get the name of the OS from os_string method. So, I simply initialized the object and created a variable to store the value returned.
my $ua = HTTP::BrowserDetect->new($user_agent);
my $os_name = $ua->os_string();
print "$user_agent $os_name\n";
there are some user agents that are not browser user agents so they won't get any value from os_string. I am getting an error Use of uninitialized value $os_name in concatenation (.) or string
How do I handle such cases when the $os_name is not initialized because the method os_string returns undef (this is what I think happens from reading the module source code). I guess there should be a way to give a default string, e.g. No OS in these cases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意:原始答案的方法(
$var = EXPRESSION || $default_value
)将为表达式返回的任何“Perl false 值”生成默认值,例如,如果表达式是空字符串,您将使用默认值。在您的特定示例中,这可能是正确的做法(操作系统名称不应该是空字符串),但在一般情况下,如果您真正想要的是仅使用默认值而不是
,那么它可能是一个错误未定义。
如果您只想避免
undef
值,而不是空字符串或零,则可以执行以下操作:Perl 5.10 及更高版本:使用“定义或运算符” (<代码>//):
Perl 5.8 及更早版本:使用条件运算符,因为已定义或尚不可用:
更深入地了解该主题(包括有关
//
的详细信息)位于 brian d foy 的帖子中: http://www . effectiveperlprogramming.com/2010/10/set-default-values-with-the-define-or-operator/Please note: the original answer's approach (
$var = EXPRESSION || $default_value
) would produce the default value for ANY "Perl false values" returned from the expression, e.g. if the expression is an empty string, you will use the default value instead.In your particular example it's probably the right thing to do (OS name should not be an empty string), but in general case, it can be a bug, if what you actually wanted was to only use the default value instead of
undef
.If you only want to avoid
undef
values, but not empty string or zeros, you can do:Perl 5.10 and later: use the "defined-or operator" (
//
):Perl 5.8 and earlier: use a conditional operator because defined-or was not yet available:
A lot more in-depth look at the topic (including details about
//
) are in brian d foy's post here: http://www.effectiveperlprogramming.com/2010/10/set-default-values-with-the-defined-or-operator/如果
$ua->os_string()
为假(即:undef
、零或空字符串),则||< 的第二部分/code> 表达式将被求值(并且将是表达式的值)。
If
$ua->os_string()
is falsy (ie:undef
, zero, or the empty string), then the second part of the||
expression will be evaluated (and will be the value of the expression).您是否正在寻找
已定义
?Are you looking for
defined
?