简体   繁体   中英

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. 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. 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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