我有一个很慢的程序,正在尝试提高性能。脚本在模块中“使用”sa sub,并将一个非常大的数组传递给 sub。经过一番修修补补,我意识到如果我将 sub 直接移动到父脚本中,并使数组全局而不是本地(所以我不必传递它),脚本会快得多(在几分钟内运行)天)。
我真的很希望能够在模块中包含该子程序(因为我有许多调用相同子程序的脚本)。但我也希望它快。:-)
半伪代码
页面.pl:
package Page;
use Star;
my @fileBytes=();
open(StarFile, "<$File");
binmode(StarFile);
while (read(StarFile, $FileValues, 1)) {
push @fileBytes, $FileValues;
}
close(StarFile);
&parseBlock(\@fileBytes);
Run Code Online (Sandbox Code Playgroud)
模块.pl:
package Star;
sub parseBlock {
my ($fileBytes) = @_;
my @fileBytes = @{ $fileBytes };
...
}
Run Code Online (Sandbox Code Playgroud)
在这里阅读一些内容:https : //www.perlmonks.org/? node = Variable%20Scoping%20in%20Perl%3A%20the%20basics 告诉我我想处理范围。因此,如果我使用“我们的”而不是“我的”来定义 @fileBytes,它将成为一个包值。据我所知,这通常在模块文件中。但我从父级的值开始。
所以我可以让父级也是一个包,定义:我们的@fileBytes
然后从模块中引用它至少像这样:@Page::fileBytes
我想我至少在理论上是正确的。
当我想使用来自不同脚本的 sub 时出现我的问题:
其他.pl:
package Other;
use Star;
my @fileBytes=();
open(StarFile, "<$File");
binmode(StarFile);
while (read(StarFile, $FileValues, 1)) {
push @fileBytes, $FileValues;
} …Run Code Online (Sandbox Code Playgroud) 我有一个号码:
11100111
我想要一个操作将我选择的特定位更改为 0。
所以,如果我希望它是:
10100111
对于第七位,我将使用什么操作,例如:
$x = 6;
$y = "11100111";
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
$z = $y & $x
Run Code Online (Sandbox Code Playgroud)
但是,我知道这是错误的。我知道我可以从该值中减去 2^$x,但这看起来不太优雅。
我正在尝试将一行旧代码从 C(我不知道)转换为 Perl(我有点知道)。
它有一个我不明白的数据结构,即一个数字后跟 /* 字符 */
if (lSeries != 18 /*S*/ && lSeries != 2 /*C*/ && lSeries != 4 /*E*/ && lSeries != 6 /*G*/) {}
Run Code Online (Sandbox Code Playgroud)