I'm looking for a way to clear all of my arrays in a Perl program.
Currently, I'm calling a subroutine that explicitly "resets" all arrays:
sub clear_arrays{(@array1,@array2,@array3)=((),(),());}
This forces me to find all the arrays in the program and literally reference them in the subroutine.
I've looked at the perldoc for reset
, undef
, and delete
but couldn't interpret any of them in a way that would clear all arrays.
Is there a built-in function of Perl that can do this?
If not, is there a function that would return an array of all the array variables?
Ex:
my @prog_arrays = getarrays();
foreach(@prog_arrays){$_ = ();}
Where getarrays()
might be a built-in Perl function that returns any/all initialized arrays in the program.
EDIT:
My particular situation involves only two global arrays that need to be reset. I broadened the question out of curiosity rather than necessity. Basically, my globals are
@email_subject
& @email_msg
.
They have values pushed into them as the script progresses and data is gathered/analyzed. At the end of the script, the email message is sent, and the script may run again depending on the loop condition variable.
If it runs again, I need to clear these 2 globals so that they can be aggregated again during the next loop cycle. It's not killing me to clear these two arrays via literal reference, but I was just wondering if Perl already had some built-in function to clear the arrays without literally referencing them.
This may not be the best way to accomplish this, but it was the first intuitive option that I considered.