Re: RFC 152 (v1) Replace $self in @_ with self() builtin (not $ME)

[email protected] (Nathan Wiger)
Newsgroups perl.perl6.language.objects
Organization Sun Microsystems
Message-ID <[email protected]>
Hildo Biersma wrote:
> 
> > =head1 TITLE
> >
> > Replace $self in @_ with self() builtin (not $ME)
> >
> 
> Don't impose your religion on others.  If people want 'this' instead of
> 'self', that should be just fine.

Whoa! I'm not imposing religion on others. FAR FROM IT! Maybe the
examples I demonstrated were too tricky and missed the main point.

Right now, a sub would be written as:

   sub mymethod {
       my $this_is_my_name = shift;
       @args = @_;
   }

Under this RFC, you would write it as:

   sub mymethod {
       my $this_is_my_name = self;
       @args = @_;
   }

The key difference is this: $_[0] always contains the first method
argument, regardless of whether you're in an object-oriented or
function-oriented context. So, if you need to support both (like CGI.pm
and my own File::Remote must), you don't have to write some special
method that detects whether $_[0] is a valid class ref (see
self_or_default in CGI.pm).

Instead, you know that all the args are always in @_:

   sub mymethod {
       my $ME = self;
       @args = @_;
   }

And you can now call this as:

   Class->mymethod
   Class::mymethod
   mymethod

And all will work without having to rewrite your sub.

This fixes a lot of stuff. Plus, "self" is one char shorter than
"shift"! ;-)

-Nate
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.