是否有一个内置的功能来清除所有的变量值 [英] Is there a built-in function to clear all variable values

查看:121
本文介绍了是否有一个内置的功能来清除所有的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来清除Perl程序我所有的阵列。

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:

子clear_arrays {(@数组1,@数组2,@ ARRAY3)=((),(),());}

这迫使我查找程序中的所有阵列和字面在子程序中引用它们。

This forces me to find all the arrays in the program and literally reference them in the subroutine.

我看了的perldoc为重置民主基金删除,但不能跨preT任何人的,将清除所有阵列的方法。

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.

有没有可以做这件事的Perl的内置函数?

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?

例如:

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

其中, getarrays()可能是一个内置的Perl函数,在程序返回任何/所有初始化数组。



编辑:的结果
我的特定情况涉及只有两个需要被重置全球阵列。我变宽了好奇,而不是必然性的问题。基本上,我的全局变量 @email_subject &安培; @email_msg

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



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.

如果它再次运行,我需要清除这些2全局以便它们可以再次在下一个循环周期内被聚集。这不是杀了我明确的文字通过参考这两个数组,但我只是想知道如果Perl中已经有一些内置的功能来清除阵列无需逐字引用它们。

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.

推荐答案

正如在其他的答案中提到,你的要求说与你的程序设计一个更大的问题。您应该使用就会掉出来的范围,或密切地管理您所有的全局数组的词法,并创建一个将清除他们为你的函数。

As mentioned in other answers, your request speaks to a larger problem with the design of your program. You should either use lexicals that will fall out of scope, or closely manage all of your global arrays, and create a function that will clear them for you.

如果你坚持在钝击您的命名空间每个数组,至少照顾和检查,以确保你是不是在写作是Perl可能需要的值:

If you insist on bludgeoning every array in your namespace, at least take care and check to make sure you aren't writing over values that Perl may 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 though:

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

的效果是有问题的阵列相同,但它不运行任何重挫你不打算的风险。你甚至可以使用我的,而不是我们与第二个例子,而第一个例子中要求的变量是在符号表(又名与定义我们)。

The effect is the same for the arrays in question, yet it does not run the risk of clobbering anything you did not intend to. You could even use my rather than our with the second example, whereas the first example requires the variables to be in the symbol table (aka defined with our).

这篇关于是否有一个内置的功能来清除所有的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆