in reply to Re^2: Perl can do it, take 1 (sentence generation)
in thread Perl can do it, take 1 (sentence generation)
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:
Even though these pairs of code are equivalent, the first of each pair is just a better habit.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"
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:
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...join "|", map quotemeta, sort { length $b <=> length $a } keys %dict;
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.
blokhead
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Perl can do it, take 1 (sentence generation)
by spurperl (Priest) on Jun 18, 2005 at 05:09 UTC |