in reply to Re^4: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ (updated)
in thread Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/

> where it would otherwise put a sub call.

which isn't trivial within strings/regexes.

the "@{[...]}" trick works but is IMHO a bit oversized for this task.

DB<109> use constant STR => 1..3 DB<110> qr/[@{[ STR ]}]/ => qr/[1 2 3]/

thats not interpolation anymore but inline eval.

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^6: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/
by AnomalousMonk (Archbishop) on Apr 30, 2014 at 11:07 UTC

    Here's another way, but I can't say it's any less klutzy. There seems to be no way to avoid the use of some kind of operator for disambiguation.

    use warnings; use strict; use constant STR => 'a string'; use constant STREF => \'ref to a string'; print qq{one way '${ \STR }' and another '${ +STREF }' \n}; print qq{and with another disambiguation '${ STREF() }' \n};

    Output:

    c:\@Work\Perl\monks>perl constant_interpolation_1.pl one way 'a string' and another 'ref to a string' and with another disambiguation 'ref to a string'