On Mon, Nov 14, 2005 at 06:59:51PM -0800, jerry gay wrote: > while adding some shiny new pge tests for return context, i came > across this PIRism: > > ##... > rulesub = p6rule('$<A>:=(.)') > match = rulesub('abc') > .local string res > res = match['A'] > print res > ## prints: a > > using keyed string access to the match object > ##... > rulesub = p6rule('$<A>:=(.)') > match = rulesub('abc') > .local string res > res = match[0] > print res > ## errors: Null PMC access in get_string() > [...] > it seems that in keyed string access to the match object, the result > is returned directly as a string. in keyed integer access to the match > object, an intermediate pmc must be used. although the workaround is > simple, the lack of symmetry seems odd. is this due to PIR > restrictions, or to PGE implementation?
Well, I suppose it can be argued either way. First, note that PGE::Match is a subclass of Hash, so one has available all of the (non-integer) keyed methods by default. The PGE::Match object then overloads some (but currently not all) of the *_keyed_int methods to be able to provide access to the array component of the Match object. Thus, while PGE::Match currently defines a C<__get_pmc_keyed_int> method, it's doesn't yet define a C<__get_string_keyed_int> method. So, a statement like .local string res .local pmc match res = match[0] is defaulting to using the inherited op from the Hash class, and since there's not an entry at the 0 key in the hash (as opposed to the array) you get the null PMC. I guess I should go ahead and provide methods in the match objects for the other *_keyed_int operations, if only to avoid this sort of confusion and the need to store things to an intermediate pmc. Another possibility may be to simply use the hash for all captures, including the "array" captures, thus removing the numbers from being valid keys. Something I read in S05 leads me to believe that $/<1> and $/[1] are actually the same, in which case we might not need the separate "array component" and *_keyed_int methods for Match objects. Pm