正则表达式 – 在使用$variable和${variable}之间传递正则表达式中的变量时Perl有什么区别

我正在审查一些用Perl编写的ClearCase触发器.我注意到在一些正则表达式中,变量既可以直接传递,也可以用大括号中的名称传递.

例如,我在触发器中有以下代码行:

if ($baseline !~ /^${component}_(|.*_)$phase\.\d+(|[a-z]|-\d+|${automateddigit})$/ &&
        $baseline !~ /^${project_root}_$phase\.\d+(|[a-z]|-\d+|${automateddigit})$/)

$component,$phase,$automateddigit,$project_root都是变量.

为什么有些传递为$variable而其他传递为正则表达式中的${variable}?

它是否来自它们的初始化方式?

以下是初始化它们的代码行:

($project = $ENV{CLEARCASE_PROJECT}) =~ s/\@.*$//;
($component = $ENV{CLEARCASE_COMPONENT}) =~ s/\@.*$//;

($project_root,$phase) = ($project =~ /^(.*)_(R\d+.*)$/);

exit(0) if (! $phase);

$phase .= ".0" if ($phase =~ /^R\d+$/);

$automateddigit = '';

$istream = `cleartool desc -fmt "%[istream]p" project:$ENV{CLEARCASE_PROJECT}`;

$componentlist = `cleartool desc -fmt "%[components]Cp" stream:$ENV{CLEARCASE_STREAM}`;
$componentsnbr = split(',',$componentlist);

if ($componentsnbr > 1) {
    $automateddigit .= '\\.\\d+';
}
如果将变量作为${name}传递,则会明确界定变量名称的结尾位置,以及引用字符串的其余部分的开始位置.例如,在您的代码中:
if ($baseline !~ /^${component}_(|.*_)$phase\.\d+(|[a-z]|-\d+|${automateddigit})$/ &&

没有{}分隔符:

if ($baseline !~ /^$component_(|.*_)$phase\.\d+(|[a-z]|-\d+|${automateddigit})$/ &&

请注意,由于正则表达式中的尾随下划线,变量$component(您可以以任何方式引用它)将被误解为$component_.

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...