Re: Dynamic method generation?
On 01/09/2011 02:26 AM, Sean McAfee wrote: On Sat, Jan 8, 2011 at 3:50 PM, Moritz Lenz mor...@faui2k3.org wrote: class MyMatcher { for FOO BAR BAZ BLETCH QUUX - $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(); }
Dynamic method generation?
Hello-- I recently wrote some Perl 5 code similar to the following, and I'm wondering what might be the analogous construction in Perl 6: 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) }; }
Re: Dynamic method generation?
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 FOO BAR BAZ BLETCH QUUX - $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