stevan / p5-mop-redux

A(nother) MOP for Perl 5
139 stars 36 forks source link

better way of accessing attributes in mop-created methods #152

Open doy opened 10 years ago

doy commented 10 years ago

Right now, to create an accessor using the mop, you need to do something like

    $meta->add_method(
        $meta->method_class->new(
            name => $attr->key_name,
            body => sub {
                my $self = shift;
                $attr->fetch_data_in_slot_for($self);
            }
        )
    );

which is going to be noticeably slower than a handwritten method like method foo { $!foo } since it has to go through the mop for every attribute access, whereas slot access is cached when accessing attributes via twigil variables. We should figure out a way around this, maybe providing an attribute($self, '$!foo') keyword or something along those lines, which would compile down to something more like the actual twigil access. In the core, we may be able to get away with using actual twigils here too, but our current twigil implementation depends on being able to compile the surrounding method ourselves.