What is the difference in Perl when passing a variable in a regular expression between using the variable $ variable and the variable $ {variable}

I am looking at ClearCase triggers written in Perl. I noticed that in some regular expressions, variables are passed either straighforwardly, or their names are in braces.

For example, I have the following line of code in a trigger:

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

$component , $phase , $automateddigit , $project_root are all variables.

Why are some passed as $variable and others passed as ${variable} in the regular expression?

Does this come from how they are initialized?

Here is the line of code that initializes them:

 ($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+'; } 
+6
syntax regex clearcase perl triggers
source share
3 answers

If you pass the variable as $ {name}, this clearly limits where the end of the variable name is and where the remaining line of the quoted string begins. For example, in your code:

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

Without delimiters {} :

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

Note that the $ component variable (you can refer to it anyway) will be misinterpreted as $ component_ due to the final underscore in the regular expression.

+7
source share

Firstly, this is called string interpolation. One good reason to use it in this case is to prevent $ project_root from being interpreted as $ project_root_ (note the underscore). It explicitly expresses the name of the variable, and does not leave it to the more complex interpolation rules.

See perldata for more information on interpolation, as well as perlre and perlop about the features of interpolation in regular expression operators.

+1
source share

As mentioned above, it must distinguish between variable names. Too many curly brackets make regular expressions even more difficult. The curly braces use their own use of regular expressions (to limit the number of matches of the pattern). I would recommend using the regexp / x modifier and rewrite your regex as:

 if ($baseline !~ /^$component # Start with $component _ # then an underscore (|.*_) # Then nothing, or anything followed by an underscore $phase # ... \.\d+ # ... (| # Then optionally: [az]| # lower alpha -\d+| # or ... $automateddigit ) $/x && $baseline !~ /^$project_root _ $phase \.\d+ (| [az]| -\d+| $automateddigit )$/x) 
+1
source share

All Articles