Substitution and interpolation with variables

Answer 1

my @rules = (
     ['^HELLO (.*)$', 'BONJOUR $1'],
);

It is better to use the natural representation of things.

The 1st element of each rule is naturally regex.

The 2nd element is naturally code.

my @rules = (
    [ qr/^HELLO (.*)$/i, sub { "BONJOUR $1" } ],
);

foreach my $rule (@rules) {
    if ($string =~ s/$rule->[0]/$rule->[1]->()/e) {
       last;
    }
}

Of course you could argue the the natural representation of the whole rule is simply CODE.

my @rules = (
   sub{ s/^HELLO (.*)$/BONJOUR $1/i },
);

for ( $string ) {
   foreach my $rule (@rules) {
      if ( &$rule ) {
    last;
      }
   }
}

Brian McCauley's original post

Next