Re: Dynamic method generation?

2011-01-09 Thread Moritz Lenz
On 01/09/2011 02:26 AM, Sean McAfee wrote:
> On Sat, Jan 8, 2011 at 3:50 PM, Moritz Lenz  wrote:
> 
>> class MyMatcher {
>>for  -> $field {
>>MyMatcher.^add_method($field, method() {
>> self!matches($field);
>>}
>>);
>>}
>> }
>>
>> The .^  means a method of the metaclass is called, here add_method
>>
>> Maybe you'll find
>> http://perlgeek.de/en/article/discovering-meta-object-protocolinteresting.
>>
>>
> I did, but now I'm wondering how one would do this in the case of an
> anonymous class. 

my $class = ClassHOW.new_class(); # just leave out the name here
for @list -> $field {
   $class.^add_method();
}


Re: Dynamic method generation?

2011-01-08 Thread Sean McAfee
On Sat, Jan 8, 2011 at 3:50 PM, Moritz Lenz  wrote:

> class MyMatcher {
>for  -> $field {
>MyMatcher.^add_method($field, method() {
> self!matches($field);
>}
>);
>}
> }
>
> The .^  means a method of the metaclass is called, here add_method
>
> Maybe you'll find
> http://perlgeek.de/en/article/discovering-meta-object-protocolinteresting.
>
>
I did, but now I'm wondering how one would do this in the case of an
anonymous class.  Neither the link you provided, nor Apocalypse or Synopsis
12, seem to address that question.  At least, scanning those pages for the
word "anonymous" didn't land me near anything that appeared to be relevant.


Re: Dynamic method generation?

2011-01-08 Thread Moritz Lenz
On 01/08/2011 09:11 PM, Sean McAfee wrote:
> package MyMatcher;
> 
> my @SIMPLE_FIELDS = qw(FOO BAR BAZ BLETCH QUUX ...);
> 
> for my $field (@SIMPLE_FIELDS) {
> no strict 'refs';
> *{ "is_\L$field" } = sub { shift->_matches($field) };
> }
> 

class MyMatcher {
for  -> $field {
MyMatcher.^add_method($field, method() {
 self!matches($field);
}
);
}
}

The .^  means a method of the metaclass is called, here add_method

Maybe you'll find
http://perlgeek.de/en/article/discovering-meta-object-protocol interesting.

Cheers,
Moritz