它们都是正则表达式。您可以在perlre 和perlretut 阅读它们。您可以在http://www.rubular.com 上与他们一起玩。
他们都隐含地对$_ 做了一些事情。在没有循环变量的代码行周围可能有一个while 或foreach。在这种情况下,$_ 成为该循环变量。例如,它可能包含正在读取的文件的当前行。
- 如果
$_ 的当前值包含+(加号)作为字符串开头的第一个字符,则#do somehting。
- 如果它包含
-(减号)符号,则为#do another thing。
在第 1 种情况下,它还会将 + 符号替换为空(即删除它)。但是,它不会删除 2 中的 -。
让我们看一下YAPE::Regex::Explain的解释。
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^\+/)->explain();
来了。在我们的案例中并没有真正的帮助,但仍然是一个不错的工具。请注意,(?-imsx 和 ) 部分是 Perl 所暗示的默认内容。除非您更改它们,否则它们始终存在。
The regular expression:
(?-imsx:^\+)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
\+ '+'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
更新:正如 cmets 中的 Mikko L 所指出的,您或许应该重构/更改这段代码。虽然它可能会做它应该做的事情,但我相信让它更具可读性是一个好主意。写它的人显然并不关心你作为后来的维护者。我建议你这样做。您可以将其更改为:
# look at the content of $_ (current line?)
if ( s/^\+// )
{
# the line starts with a + sign,
# which we remove!
#do something
}
elsif ( m/^-/ )
{
# the line starts witha - sign
# we do NOT remove the - sign!
#do another thing
}