I dread +shift (and anything with a + to force scalar context, it just looks so kludgy...), I guess you could use $_[0] instead ?
In this context, a lot of people prefer using "shift()" or the even more explicit "shift @_" instead.
Why are you using quotemeta ?
I consider it similar to the following Good Habits of 3-arg open and the list form of system:
open my $fh, "<", "hard-coded-filename"; # instead of: open my $fh "<hard-coded-filename" system qw[somecommand -o -p -q]; # hard-coded arguments # instead of: system "somecommand -o -p -q"
Even though these pairs of code are equivalent, the first of each pair is just a better habit.

In my previous post, I'm constructing a string that will be interpreted as a regular expression, and I want to reinforce the fact that the keys of %dict should be matched as exact strings. If one of the keys had a period or a curly brace, or square brackets, these need to be escaped. So even though I know that all the keys that I put in %dict are safe to begin with, I do quotemeta anyway for extra B&D good karma.

And to be extra safe, to construct a list of alternations, one should usually sort by decreasing length as well:

join "|", map quotemeta, sort { length $b <=> length $a } keys %dict;
Since the regex engine tries alternations from left to right, if any of the keys were a substring of another key, we would need to list the longer key first. Otherwise we would never match the longer key...

Or in this case, since the keys are all \w characters, we could put \b's around ($nonterminal) to force it to match a maximal \w+ word.


In reply to Re^3: Perl can do it, take 1 (sentence generation) by blokhead
in thread Perl can do it, take 1 (sentence generation) by spurperl

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