in reply to <p>string variable</p>
Others have said, and I agree, that what you seem to want to do (which, by the way, is generally called a "templating system"; see, e.g., Text::Template) is, in the context in which you want to do it, a Bad IdeaSM. (But there's nothing wrong with the general notion of templating systems — given the right application!) An example of a quick-and-dirty implementation is given below.
Others have suggested some variation on the use of hashes, and hash access is part of the "named capture group" facility of the regex engine of Perl versions 5.10+. See the discussion of the (?<NAME>pattern) construct in the Extended Patterns section of perlre, and also the example below. See also the associated %- and %+ hashes in perlvar. Named capture groups can be quite convenient, but in regexes that do a lot of work, there may be a significant price to pay: YMMV.
>perl -wMstrict -le "my @array = qw(A1 A2 A3 $1 $2); ;; my $message = 'If you choose a yellow car everyone will follow you!'; my $my_trigger = qr{If you (.*) a (?<COLOR>.*) car (?<WHOM>.*) will follow you!}; ;; if ($message =~ $my_trigger) { ;; my $option = eval qq{ qq{$array[3]} }; my $color = qq{color was: '$-{COLOR}[0]'}; my $whom = $+{WHOM}; ;; print qq{option of '$array[3]' was: '$option'}; print $color; print qq{whom: '$whom'}; } else { print 'no match'; } " option of '$1' was: 'choose' color was: 'yellow' whom: 'everyone'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: <p>string variable</p>
by coss (Initiate) on Jul 24, 2013 at 16:48 UTC |