Is there a built-in function to clear all variable values

I am looking for a way to clear all my arrays in a Perl program.

I am currently calling a routine that explicitly "flushes" all arrays:

sub clear_arrays{(@array1,@array2,@array3)=((),(),());}

This makes me find all the arrays in the program and literally refer to them in the subroutine.

I looked at perldoc for reset , undef and delete , but couldn't interpret them to clear all arrays.

Is there a built-in Perl function that can do this?

If not, is there a function that returns an array of all array variables?

Example:

 my @prog_arrays = getarrays(); foreach(@prog_arrays){$_ = ();} 

Where getarrays() can be a Perl built-in function that returns all / all initialized arrays in a program.


EDIT:
In my specific situation, only two global arrays are involved, which should be reset. I expanded this question out of curiosity, not out of necessity. Basically, my global values ​​are @email_subject and @email_msg .

They have values ​​that are inserted into them as the script progresses, and the data is collected / analyzed. An email is sent at the end of the script, and the script may run again depending on the loop condition variable.

If it starts up again, I need to clear these 2 globals so that they can be aggregated again during the next cycle of the cycle. It doesn’t kill me to clear these two arrays using a literal link, but I was just wondering if Perl has a built-in function to clear arrays without referencing them.

This may not be the best way to accomplish this, but it was the first intuitive option that I considered.

+4
source share
3 answers

As mentioned in other answers, your request indicates a bigger problem with the design of your program. You must either use vocabulary that goes out of scope, or closely manage all your global arrays and create a function that clears them for you.

If you insist on duplicating each array in your namespace, at least take care and make sure you don't write the values ​​that Perl might need:

 for (keys %::) { # for everything in `package main;` if (*{$::{$_}}{ARRAY}) { # if there is an array in the slot # clear it unless it is a "special" array @{$::{$_}} = () unless /^(?:INC|ISA|EXPORT|EXPORT_OK|ARGV|_|\W)$/ } } 

I would write it like this:

 my @global_arrays = \our (@foo, @bar, @baz); sub clear_global_arrays { @$_ = () for @global_arrays } 

The effect is the same for the arrays in question, but it does not run the risk of knocking down everything that you did not intend to. You can even use my rather than our with the second example, while the first example requires the variables to be in the character table (also known as our ).

+6
source

Do not use global arrays. It is so simple. Lexical arrays are limited to the area in which they are declared, and are automatically started empty when entering the area.

If you must use global variables, tracking them all in one place is a good idea, so clearing them should not be difficult.

Someone once posted a now infamous tool for perlmonks to do what you want. The code was recalled after receiving great criticism of the whole idea; you can read some criticisms here: http://www.perlmonks.org/index.pl?node_id=349496

+19
source

The fact that you want this scream “bad design” for me. However, on the assumption that you know exactly what you are doing with this radioactive chainsaw, you can execute it by accessing the global hash table of characters %:: or %main:: . (Colons are part of the name.) This hash contains a mapping from each specific global character to a reference to its variable.

Something like this should be enough:

 for my $ref (values %::) { @{$ref} = (); } 

Edited to remove check for array references. All values ​​are actually global type references, so there is no need to check.

+8
source

All Articles