"Scalar found where operator expected" in Perl substitution

99 Views Asked by At

I'm trying to increment a version number in a file using Perl. I want to change this line:

Bundle-Version: 1.0.2

to

Bundle-Version: 1.0.3

I thought I could do this using the following regex substitution:

perl -i -pe 's/(Bundle-Version: \d+.\d+.)(\d+)/$1$2+1/e;' MANIFEST.MF

But I get the following error:

Scalar found where operator expected at -e line 1, near "$1$2"
(Missing operator before $2?)
syntax error at -e line 1, near "$1$2"
Execution of -e aborted due to compilation errors.

Could somebody please advise what I am doing wrong here?

1

There are 1 best solutions below

1
Wiktor Stribiżew On

The /e flag makes Perl treat the RHS as an expression, so you might fix your current code using 's/(Bundle-Version: \d+.\d+.)(\d+)/$1 . ($2+1)/e;', but you can simplify it a bit and use

perl -i -pe 's/Bundle-Version:\s*\d+\.\d+\.\K(\d+)/$1+1/e;' MANIFEST.MF

See the online demo.

Details:

  • Bundle-Version: - a fixed string
  • \s* - zero or more whitespaces
  • \d+\.\d+\. - two sequences of one or more digits and a dot
  • \K - match reset operator that discards the text matched so far from the overall match memory buffer
  • (\d+) - Group 1 ($1): one or more digits