gpt4 book ai didi

perl - 未加载 @ISA 中的包时多次发出相同警告

转载 作者:行者123 更新时间:2023-12-04 08:52:31 26 4
gpt4 key购买 nike

在重构我的一些 perl 代码时,我
注意到以下奇怪的行为。
考虑这个小示例脚本:

#!/usr/bin/perl -w

package test;
use strict;
my $obj = bless( {}, __PACKAGE__);
our @ISA = qw( missing );

exit(0)

预期的警告
Can't locate package missing for @test::ISA at test.pl line 8

出现了 3 次,而不是只出现了 1 次。此警告的其他两个触发器是什么?所有三个引用 exit(0) .
gentoo linux 上的 perl 版本 5.12.4。

谢谢。

最佳答案

我相信 tjd 得到了答案。如果您将 DESTROY() 和 AUTOLOAD() 方法添加到测试包中,您只会收到一个警告(关于缺少 @test::ISA)。

   package test;
use strict;

sub DESTROY {}
sub AUTOLOAD {}

您通常需要确保@ISA 列表中列出的包已经被加载。在您的示例中,您可能希望看到如下内容:
   package test;  
use strict;
use missing;

our @ISA = ('missing');

有点好奇你的包没有明确的 new() 方法。相反,您有一个调用 bless(); 的语句。

如果你有一个 new() 方法,像这样:
   sub new() { return bless {}, __PACKAGE__; }

然后你不会看到三重错误消息,直到调用 new();
   package main;
my $object = test->new(); # Triple warning

您可能想要使用 pragma 'base',这会给您一个 fatal error ,说它无法加载 'missing' 包:
   package test; 
use strict;
use base ('missing');

sub new { bless {}, __PACKAGE__);

基本编译指示会尝试为您加载丢失的包。您不需要单独的“使用缺失”语句。

所以最后的事情可能看起来像这样:
   package test;
use strict;
use warning;
use base ('missing');

sub new { bless {}, __PACKAGE__);
sub DESTROY {}
sub AUTOLOAD {}
1;

然后,您可以将这一切包装到一个名为 test.pm 的文件中并在脚本中使用它:
   use strict;
use test;

my $object = test->new(); # Should fatal, as there is no 'missing' module

希望这可以帮助。

关于perl - 未加载 @ISA 中的包时多次发出相同警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19905142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com