简体   繁体   English

如何在Perl中强制将对象属性设置为完整数组,而不仅仅是其长度?

[英]How can I force an object attribute in Perl to be set to a full array instead of just its length?

I have an object I defined with a method, childNodes(), which returns an array. 我有一个用方法childNodes()定义的对象,该方法返回一个数组。 When I do something like: 当我做类似的事情时:

my @arr = obj->childNodes()

I can clearly see that it can properly return an array. 我可以清楚地看到它可以正确地返回一个数组。

My problem is that when I try to use this method to set the attribute of another class object, Perl decides I just want the length of childNodes() rather than the full array. 我的问题是,当我尝试使用此方法设置另一个类对象的属性时,Perl决定我只想要childNodes()的长度,而不是整个数组的长度。 This is not at all what I want and ruins everything. 这根本不是我想要的,并且毁了一切。 The code I'm using for this is: 我为此使用的代码是:

$self->{'_arr'} = obj->childNodes()

How can I make this set $self->{'_arr'} to an array instead of just a scalar number? 如何将$ self-> {'_ arr'}设置为数组,而不只是标量数字?

Thanks in advance! 提前致谢!

When you evaluate an array in scalar context, it returns the length of the array. 当您在标量上下文中求值数组时,它将返回数组的长度。

You want a reference to the array: 您要引用该数组:

$self->{'_arr'} = [ obj->childNodes() ];

See perldoc perlref . 请参见perldoc perlref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM