我正在维护一些通过串行无线电读取值的代码,并将它们解压缩到Perl数据结构中:
# Don't yell at me, I didn't write this
if ($command_string =~
/^.(.)(.).(..)(.)(..)(.)(....)(....)(....)(....)
(..)(..)(.)(.)(.)(.)(..)(..)(..)(..)(..)(..)(.)(.).......
(.)........(.)(.).*/sx) {
$config->{sequence} = hex(unpack('H2', $1));
$config->{radio_id} = hex(unpack('H2', $2));
...
$config->{radio_type} = hex(unpack('H2', $26));
$config->{radio_channel} = hex(unpack('H2', $27));
}
Run Code Online (Sandbox Code Playgroud)
这个笨重的捕获正则表达式让我想知道:Perl中编号捕获变量的上限是什么?它会一路走$MAXINT吗?
我正在开发一个项目,它一次从ftp服务器获取文件列表.此时,它返回文件的arrayref,或者如果qr传递了可选的正则表达式引用(即),则使用grep过滤列表.此外,如果它qr有一个捕获组,它会将捕获的部分视为版本号,而是返回一个hashref,其中键是版本,值是文件名(如果没有捕获组,它将作为数组返回).代码看起来像(稍微简化)
sub filter_files {
my ($files, $pattern) = @_;
my @files = @$files;
unless ($pattern) {
return \@files;
}
@files = grep { $_ =~ $pattern } @files;
carp "Could not find any matching files" unless @files;
my %versions =
map {
if ($_ =~ $pattern and defined $1) {
( $1 => $_ )
} else {
()
}
}
@files;
if (scalar keys %versions) {
return \%versions;
} else {
return \@files;
}
} …Run Code Online (Sandbox Code Playgroud)