Hello,

Consider this code:

use strict; use warnings; my $BEL_LETTERS = qr/ABVHD/; my @all_words = qw(tak het hen toj tah tam tym); ... while (my $next_line = <$FH>) { foreach my $next_word (@all_words) { my $count_words = ($next_line =~ s/\b($next_word[$BEL_LETTERS]*)\b/>$1</gi); } }

The substitution looks for words that begin with any of @all_words and end with zero or more of $BEL_LETTERS.

This code doesn't compile. Problem: the compiler thinks that $next_word[$BEL_LETTERS] is member $BEL_LETTERS of array @next_word.

This works - notice the parentheses:

s/\b($next_word([$BEL_LETTERS])*)\b/>$1</gi);

...But it feels like a hack to me. Is there a better way to overcome this interpolation?

I tried searching for the answer here in Q&A and in the Camel book, and strangely couldn't find it. I'm quite sure that the answer must be in the book, and if someone can point it to the page i'll be very thankful too.

(If anyone is curious, this code is looking for pronouns in a Belarusian text. This example here is very simplified - i didn't want to put the whole Belarusian-Cyrillic alphabet here).


In reply to variables in regex character classes by amir_e_a

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.