On Mon, Apr 24, 2006 at 04:50:43PM +0300, Markus Laire wrote:
> In Synopsis 5 (version 22),
> 
> Under "Variable (non-)interpolation" it's said that
> <quote>
> An interpolated hash matches the longest possible key of the hash as a
> literal, or fails if no key matches. (A "" key will match anywhere,
> provided no longer key matches.)
> </quote>
> 
> And under "Extensible metasyntax (<...>)" it's said that
> <quote>
> With both bare hash and hash in angles, the key is counted as
> "matched" immediately; that is, the current match position is set to
> after the key token before calling any subrule in the value. That
> subrule may, however, magically access the key anyway as if the
> subrule had started before the key and matched with <KEY> assertion.
> That is, $<KEY> will contain the keyword or token that this subrule
> was looked up under, and that value will be returned by the current
> match object even if you do nothing special with it within the match.
> </quote>
> 
> I don't quite understand how these relate to each other. First text is
> clear enough, but second seems to be something totally different.

Indeed, they are saying different things about what happens when you
match a hash against a string.

> Could someone give an example of what difference there's between
> "interpolated hash matches the longest possible key of the hash as a
> literal, or fails if no key matches." and "the key is counted as
> "matched" immediately; that is, the current match position is set to
> after the key token before calling any subrule in the value. ..."
> 
> I don't quite understand if the key is matched in the second version
> or if it's just counted as "matched", whatever that means, and why the
> description is so dis-similar to the first quote.

What those two passages are saying is that when you match with a hash, 
the longest key that matches will trigger the value portion of the
hash to execute (what "execute" means depends on the nature of the
value. See S05 for more details) Given, for example, the following:

    my %hash = ( 
       'foo'    => ...,
       'food'   => ...,
    );

    "I need some food for breakfast" ~~ /%hash/;

That first passage says that "food" must match since it's the longest
key that matches, so whatever the ... for the "food" key is will
execute.  The second passage says that the "match cursor" is
placed just after the key that matched.  So any subsequent matches to
that string that do not reset the cursor will start just after the
word "food".

But what if your subrule needs to know exactly which key matched or
needs to match the key again for some reason? The second passage says
that you may access they actual text that matched with $<KEY> and you
may again match the actual key that matched with the <KEY> assertion. In
my example, $<KEY> will contain the text "food" . Also, by using the
<KEY> assertion, you can start matching at the beginning of the key
(rather than just after it) and again match the same key of the hash
that caused the match to succeed in the first place.

<speculation>
Why would you need to match the key again? Maybe your subrule needs to
know what came before the key in order to perform some action:

        req() if m:c/ <after need .*?> <KEY> /          # require
        des() if m:c/ <after want .*?> <KEY> /          # desire

I assume that <KEY> is somehow magically anchored to the spot where
the key actually matched.
</speculation>

-Scott
-- 
Jonathan Scott Duff
[EMAIL PROTECTED]

Reply via email to