- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个使用 Class::ArrayObjects 的简单程序,但它没有按预期工作。程序是:
TestArrayObject.pm:
package TestArrayObject;
use Class::ArrayObjects define => {
fields => [qw(name id address)],
};
sub new {
my ($class) = @_;
my $self = [];
bless $self, $class;
$self->[name] = '';
$self->[id] = '';
$self->[address] = '';
return $self;
}
1;
Test.pl
use TestArrayObject;
use Data::Dumper;
my $test = new TestArrayObject;
$test->[name] = 'Minh';
$test->[id] = '123456';
$test->[address] = 'HN';
print Dumper $test;
当我运行Test.pl时,输出数据是:
$VAR1 = bless( [
'HN',
'',
''
], 'TestArrayObject' );
我想知道“姓名”和“身份证”的数据在哪里?
谢谢,明。
最佳答案
始终使用use strict
。尽量使用use warnings
。
使用 use strict
你的测试脚本甚至不会运行,Perl 将发出以下错误消息:
Bareword "name" not allowed while "strict subs" in use at test.pl line 8.
Bareword "id" not allowed while "strict subs" in use at test.pl line 9.
Bareword "address" not allowed while "strict subs" in use at test.pl line 10.
Execution of test.pl aborted due to compilation errors.
这是因为数组索引的名称仅对 TestArrayObject 模块可见,而对测试脚本不可见。
为了使您的类保持面向对象,我建议您为变量实现访问器,例如 get_name/set_name 并从您的类模块外部使用这些访问器。
关于perl - 如何使用 Class::ArrayObjects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/605641/
我试图理解文档中的 STD_PROP_LIST 常量,但到目前为止我不明白,也没有找到任何解释:(该文档有以下示例: $a = new ArrayObject(array(), ArrayObject
如果是,那么它与使用 arrayObject = [] 有何不同.这是两件事不同还是两种做同样的事情。 最佳答案 array.length = 0; 保留对原始数组的对象引用。 array = [];
我收到了这条通知: ArrayIterator::next(): Array was modified outside object and internal position is no longe
我想了解什么是getIterator(),我会解释一下: 据我所知,getIterator 是我们调用以包含外部迭代器的方法。 问题是 getIterator 包括它自己的方法 关闭认为看起来与 It
问题: 当手动设置或读取值时,ArrayObject 会按预期工作,但是当使用函数(例如 foreach)对其进行迭代时,事情会变得很糟糕。 它不调用我定义的 offset* 方法,而是使用 Arra
我在哪里可以找到 ArrayObject 的完整源代码(PHP 格式)? 我不明白的是为什么在向 ArrayObject 添加元素时可以使用“箭头”,例如: $a = new ArrayObject(
应该使用哪一个来操作数据,数组还是数组对象?像搜索、排序和其他数组操作。 最佳答案 基本类型是array。这是可以在循环中写入、读取和访问的键和值的映射。 ArrayObject 是一个类,您可以扩展
我正在尝试存储一个数组并使用扩展 ArrayObject 的自定义类来操作该数组。 class MyArrayObject extends ArrayObject { protected $d
我编写了一个使用 Class::ArrayObjects 的简单程序,但它没有按预期工作。程序是: TestArrayObject.pm: package TestArrayObject; use C
ideone 示例代码: array('d')); print_r($a); unset($a['b']['c']); print_r($a); 输出 ArrayObject Object (
我一直在寻找一种方法来实现 ArrayObject 类来存储应用程序配置,我在 php 手册(评论之一)中找到了这个实现 setFlags(ArrayObject::ARRAY_AS_PROPS);
我正在尝试访问公共(public)函数 get_data(),以生成“此处”的输出,以查看是否从对象数组创建动态对象..那么我该怎么做。 #include #include using namesp
我正在做 FRVT 1:1 验证。所以我需要使用FRVT提供的程序。我已经连接到我编写的程序,并完成了实现。 但是我想把我用cython写的东西一步步移植到NullImp FRVT 提供的示例。但我得
这个问题已经有答案了: Type safety: Unchecked cast (10 个回答) 已关闭 7 年前。 我该如何解决这个警告? Type safety: Unchecked cast f
如标题所述,如何在 arrayObject 上执行 array_unshift(),array_push() 是通过执行arrayObject->append() 但是 unshift 呢? 编辑:我
问题:我正在尝试扩展 PHP 的 ArrayObject,如下所示。不幸的是,在设置多维对象时我无法让它正常工作,而是抛出了一个错误,因为我在 PHP 中启用了严格设置。 (错误:严格标准:从空值创建
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在 ArrayObject 中寻找 __destroy() 方法,但没有找到实现。如果我将包含 ArrayObject 的变量设置为 NULL,它会正确地销毁存储在其中的所有对象并释放内存吗?或者我
我不知道如何使用 SwiftyJSON.JSON 制作数组我可以使用 SwiftyJSON.JSON 解析结构,但无法解析对象数组。请帮助我。 JSON { "result": { "li
我发现很多类似的问题,人们对这个文件有疑问。我知道头文件在哪里,我用 -I 解决 g++,因为人们通常会回答前面的问题。我做了所有这些,但他们没有工作。 1 , 2 sum_array_wrap.cp
我是一名优秀的程序员,十分优秀!