[Israel.pm] Implicit $self variable without Source Filters

Gaal Yahas gaal at forum2.org
Mon Nov 6 11:46:43 PST 2006


On Mon, Nov 06, 2006 at 09:34:59PM +0200, Shlomi Fish wrote:
> During the last meeting we discussed the fact that Moose was pretty cool, but 
> it still didn't have the Spiffy feature in which one does not have to write 
> my $self = shift; at the beginning of methods. Now Spiffy uses a source 
> filter for this.

Check out Module::Compile and Filter::Simple::Compile.

> Today this made me think whether it can be achieved without a source filter. 
> And indeed it can. By wrapping the method in a closure which localises $self 
> to the first argument (and passes the rest to the closure), one can have an 
> implicit $self. Here's the code:
> 
> The downsize of this is that $self is a global and that the SUPER, NEXT, etc. 
> meta-methods may be broken. But I still think it's pretty cool.

The more immediate downside is perhaps that since $self is not a lexical,
it cannot be further closed upon. Unless it is copied, but that sort of
misses the point of this magic. Still, this is pretty cool.

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list