gpt4 book ai didi

perl - 类::结构错误:无法定位对象方法

转载 作者:行者123 更新时间:2023-12-04 08:00:36 24 4
gpt4 key购买 nike

为了简化一些现有的工具,我想用一个结构数组替换几个数组。所以我做了一个简单的示例代码来测试结构的创建。使用的代码如下:

use strict;
use warnings;

#package Net_Node;
use Class::Struct Net_Node => [host => '$', access => '$', pass => '$'];
#struct( host => '$', access => '$', user => '$', pass => '$' );

my $example = new Net_Node;

$example->host('dir ip');
$example->access('TELNET');
$example->user('hjack');
$example->pass('buttstalion');

print "\n\n\n";
print "${example->host}\n";
print "${example->access}\n";
print "${example->pass}\n";

问题是当我尝试执行这个脚本时,我得到了这个错误:

Can't locate object method "host" via package "example" (perhaps you forgot to load "example"?) at test_2.pl line 15.

你能帮我看看我需要更正什么吗??

最佳答案

阅读 Class::Struct EXAMPLES ,看起来您需要调用 struct 函数:

use warnings;
use strict;
use Class::Struct;

struct(Net_Node => [host => '$', access => '$', pass => '$']);

my $example = Net_Node->new();

$example->host('dir ip');
$example->access('TELNET');
$example->pass('buttstalion');

print $example->host, "\n";
print $example->access, "\n";
print $example->pass, "\n";

这打印:

dir ip
TELNET
buttstalion

此外,$example->host 似乎不会插入双引号内。

关于perl - 类::结构错误:无法定位对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66498906/

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