Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Pass array, then clear

by pwagyi (Monk)
on Jan 16, 2018 at 02:13 UTC ( [id://1207330] : note . print w/replies, xml ) Need Help??

in reply to Pass array, then clear

If you have knowledge with pointer (C/C++) it is pretty much the same in Perl.

sub clear { my ($ref) = @_; $ref = []; # now ref is made to point to new object (array) @$ref = (); # what ref points to is modified }

$ref is a pointer(reference) variable, it stores only address(so as to speak). So when $ref is assigned to point to another object, subroutine caller does not see the change. On the other hand, if you assign what $ref points to (deference), the change is reflected in caller.