I have the following line in my Perl script:
s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /g;
I want to find all words in the string and if the word is in the array of known words replace it else keep it (ideally I want to do an arbitrary operation on match, not just ternary operator).
To do so I attempt to use ternary operator. Perl treats ? and : as literal symbol and just concats these with variables (if defined).
How do I cause Perl to treat ?: inside the replace as ternary operator?
P.S: I know that I can just perform operation on match in the next line of code but I want to keep it one liner for clarity.
You need the 'e' (execute) qualifier:
s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /ge;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With