From: Dan Sugalski Date: 2002-01-21T07:07:04+09:00 Subject: Re: dynamic method creation At 5:54 AM +0900 1/21/02, Chr. Rippel wrote: >"Dan Sugalski" wrote in >.... >> >Write how you do this in Perl without using eval. You can do it with eval >> >in Ruby, but the original posters would like something that uses not eval >> >(not sure why). >> >> Sure. >> >> If your methods (or subs, whatever) can be synthesized at runtime by >> just changing data and building a closure, you can do something like: >> >> sub make_getter { >> my $element = $_[0]; >> *{"__PACKAGE__::get_$element"} = sub {return $_[0]->{$element}}; >> } > >Someone correct me if am wrong but that seems to be Perl's >version of Ruby's Module#define_method in 1.7.? Got me with that one--hopefully someone more experience could fill in the details. It's a really useful feature, especially in conjunction with a fallback method that gets called for methods that don't exist. You could probably, if you wanted and your system supported it, dynamically generate named system calls and suchlike stuff too, either at startup time or dynamically as needed. -- Dan --------------------------------------"it's like this"------------------- Dan Sugalski even samurai dan@sidhe.org have teddy bears and even teddy bears get drunk