Search Results

Search found 2 results on 1 pages for 'arareko'.

Page 1/1 | 1 

  • passing Perl method results as a reference

    - by arareko
    Some XML::LibXML methods return arrays instead of references to arrays. Instead of doing this: $self->process_items($xml->findnodes('items/item')); I want to do something like: $self->process_items(\$xml->findnodes('items/item')); So that in process_items() I can dereference the original array instead of creating a copy: sub process_items { my ($self, $items) = @_; foreach my $item (@$items) { # do something... } } I can always store the results of findnodes() into an array and then pass the array reference to my own method, but let's say I want to try a reduced version of my code. Is that the correct syntax for passing the method results or should I use something different? Thanks!

    Read the article

  • How can I pass an array resulting from a Perl method by reference?

    - by arareko
    Some XML::LibXML methods return arrays instead of references to arrays. Instead of doing this: $self->process_items($xml->findnodes('items/item')); I want to do something like: $self->process_items(\$xml->findnodes('items/item')); So that in process_items() I can dereference the original array instead of creating a copy: sub process_items { my ($self, $items) = @_; foreach my $item (@$items) { # do something... } } I can always store the results of findnodes() into an array and then pass the array reference to my own method, but let's say I want to try a reduced version of my code. Is that the correct syntax for passing the method results or should I use something different? Thanks! EDIT: Now suppose I want to change process_items() to process_item() so I can do stuff on a single element of the referenced array inside a loop. Something like: $self->process_item($_) for ([ $xml->findnodes('items/item') ]); This doesn't work as process_item() is executed only once because a single value is passed to the for loop (the reference to the array from findnodes()). What's the proper way of using $_ in this case?

    Read the article

1