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

前端之家收集整理的这篇文章主要介绍了perl – 无论如何,都会认为字符串重载变量已定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的脚本中有以下几行:

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 perl

use 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..2
ok 1 - $obj_str_defined is defined
ok 2 - $obj_str_undef is undef

猜你在找的Perl相关文章