DEV Community

String Interpolation of Constants in Perl 5

Kirk Lewis on October 01, 2018

This post demonstrates how to use Perl 5 constants with String Interpolation and Regular Expressions. I will briefly explain how String Interpolati...
Collapse
 
ribugent profile image
Gerard Ribugent Navarro

I didn't know this way to interpolate variables using this notation, also I thought it was impossible to interpolate constants, for this reason at the work we switched from const pragma to Readonly as suggested by the ProhibitConstantPragma perl critic policy

Collapse
 
choroba profile image
E. Choroba

The important feature of constants is that they are inlined during the compile time, unlike Readonly. Interestingly,

"${ \CONSTANT }"

still inlines the value, but not the reference and dereference:

$ perl -Mconstant=ABC,42 -MO=Deparse -e 'print ABC, "${\ABC}"'
print '42', "${\'42';}";