perl – 无论如何,都会认为字符串重载变量已定义

perl – 无论如何,都会认为字符串重载变量已定义,第1张

概述我的脚本中有以下几行: my $spec = shift;if (!defined $spec) { return ("Invalid specification", undef);}$spec = "$spec" // ''; 我自然希望这会在传递undef时返回数组中的警告Invalid specification,第二项是undef.相反,检查通过,我得到一个控制台消息警告我在 我的脚本中有以下几行:

my $spec = shift;if (!defined $spec) {    return ("InvalID specification",undef);}$spec = "$spec" // '';

我自然希望这会在传递undef时返回数组中的警告InvalID specification,第二项是undef.相反,检查通过,我得到一个控制台消息警告我在下一行使用未初始化的值$spec in string.

$spec是一个带字符串和数字重载的对象,不幸的是这样编写,试图在这个特定的子例程中测试真实性(例如if($spec))会导致深度递归和段错误.

虽然我感兴趣的是,为什么,确切地说,这种情况正在发生,但我更感兴趣的是如何让它停止发生.我想消除控制台警告,最好没有警告qw / uninitialized /.这是可能的,如果是的话,我该怎么做?

解决方法 你说$spec是一个 string overloading的对象.

如果是这种情况,那么在检查它是否被定义之前,您需要将其强制转换为String形式:

if (! defined overload::StrVal($spec)) {

每月更正

正如在StrVal中指出的那样,没有强制重载的字符串化:

overload::StrVal(arg)

Gives the string value of arg as in the absence of stringify overloading. If you are using this to get the address of a reference (useful for checking if two references point to the same thing) then you may be better off using Scalar::Util::refaddr(),which is faster.

因此,要实现这一点,请尝试他的另一个建议:

“$spec” trapPing warnings and detecting the uninitialized var warning. Better to add a method to the class to test for whatever case returns undef.

以下演示了这种方法:

#!/usr/bin/env perluse strict;use warnings;use Test::More tests => 2;my $obj_str_defined = StringOverloaded->new("has value");my $obj_str_undef   = StringOverloaded->new(undef);ok( is_overloaded_string_defined($obj_str_defined),qq{$obj_str_defined is defined} );ok( !is_overloaded_string_defined($obj_str_undef),qq{$obj_str_undef is undef} );sub is_overloaded_string_defined {    my $obj = shift;    my $is_str_defined = 1;    local $SIG{__WARN__} = sub {        $is_str_defined = 0 if $_[0] =~ /Use of uninitialized value $obj in string/;    };    my $throwaway_var = "$obj";    return $is_str_defined;}{    # Object with string overloading    package StringOverloaded;    use strict;    use warnings;    use overload (        '""' => sub {            my $self = shift;            return $$self;    # Dereference        },fallback => 1    );    sub new {        my $pkg  = shift;        my $val  = shift;        my $self = bless $val,$pkg;        return $self;    }}

输出:

1..2ok 1 - $obj_str_defined is definedok 2 - $obj_str_undef is undef
总结

以上是内存溢出为你收集整理的perl – 无论如何,都会认为字符串重载变量已定义全部内容,希望文章能够帮你解决perl – 无论如何,都会认为字符串重载变量已定义所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1245139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存