self::implicit, they easy way to emulate implicit method invocation.

作者:   發佈於:  

About 1.5 years ago I figured how to emulate implicit method invocation at runtime, but I never released it as a cpan module. And here it comes: self::implicit.

It's a part of self distribution, the context variable $self is automatically figured out based on the type first argument. For example, consider the following code:

sub single { $self->{number} }
sub double { $self->single * 2 }

That double routine can be simplified to be:

sub double { single() * 2 }

If we declare sub prototype to be an empty list, they can be invoked without the empty argument list:

sub single() { $self->{number} }
sub double { single * 2 }

self::implicit should not be used together with self, for you will end up with double-injected code that mess up the value of @_. It also declares $self and @args variable for you to use like self, so there really is no reason to say use self once use self::implicit is there.

Even though I put it together and released it, I'm not sure how it should evolve at the moment. It is just that this language feature is built in Ruby, and I think Perl programmers should someday be benefit from not having to say $self all the time. An OO language can force you to say self or this too much, while a functional language can force you to carry context information in its long argument lists. By properly digging $self variable from the caller scope, it makes programming much easier because the expressions that must be said is less.