This and other RFCs are available on the web at http://dev.perl.org/rfc/ =head1 TITLE Parse C<func($obj, @args)> as C<func $obj (@args)> =head1 VERSION Maintainer: Nathan Wiger <[EMAIL PROTECTED]> Date: 29 Aug 2000 Mailing List: [EMAIL PROTECTED] Version: 1 Number: 174 Status: Developing =head1 ABSTRACT Currently, Perl 5 makes a distinction between subs called in the indirect object vs. function form: $r = new CGI; # CGI->new $r = new CGI (@args); # CGI->new(@args) $r = new(CGI @args); # mail::new(main::CGI(@args)) $r = new(CGI, @args); # main::new(CGI, @args) This causes many problems, such as having to have special prototypes that translate the optional first argument object into the indirect object syntax. In Perl 6, this distinction should be dropped. Instead, the second form should be automatically translated to the first when necessary. This fixes a lot of stuff, and breaks nothing. Read on... :-) =head1 DESCRIPTION =head2 Overview Let's get into a more subtle example: close $FILE; # $FILE->close as in L<RFC 14> close($FILE); # main::close($FILE) Here, we have to include a function in CORE called C<close> which, at the very least, just turns around and reforks the appropriate method call. While this works for C<close> (as implemented currently), if we want user-level consistent behavior, our other functions "don't work right": close(FILE); # ok $r = new(CGI); # nope This should be made to work correctly. The following order of function parsing should apply to Perl 6: 1. The current package should be checked for a function by that name (including any imported ones), consistent with current behavior. 2. Argument 1 should be checked to see if it is a valid object reference. If so, the method should be checked for existence as a member. 3. AUTOLOAD should be called. The only new step here is step #2. Notice that this does NOT override current-package functions. Only if a function is not found in the current package is the translation attempted. =head2 Why this is Needed At first, this may seem like syntactic sugar. It's not, it's much more than that. In fact, this is needed to make Perl 6 much more flexible and to reduce the number of core methods. It also meshes completely with RFC 161 on embedded objects. In fact, every existing method can become a member function if we so desire: $xa = abs($x); # main::abs($x) || $x->abs $r = new(CGI, @args); # CGI->new(@args) dostuff(@stuff); # $stuff[0]->dostuff(@stuff[1..$#stuff]) This adds a great amount of flexibility and transparency for users. In fact, C<close> and C<open> could leave core altogether, instead being called on the objects: $FILE = open http "http://www.yahoo.com/"; # http->open close($FILE); # $FILE->close While this is by no means required, it should be obvious what the benefits of being able to do this are. It makes the core syntax much more flexible, and reduces the need to include simple translator prototypes. It also makes L<RFC 168>, "Built-in functions should be functions", much more realistic since these prototypes no longer have to handle the special case of an "optional first argument object". =head2 Syntax Just to clarify the syntax some, the following three should be equivalent: $r = new CGI (@args); # CGI->new(@args) $r = new(CGI, @args); # CGI->new(@args) $r = new(CGI @args); # CGI->new(@args) Note that the comma following the object is optional. These two cases actually involve subtly different changes to Perl's parsing rules. The third one will try to find a C<main::CGI> function and call it on C<@args>. I would say that only if this fails, and C<main::CGI> is not found, should the translation be attempted. As such, the third form would require an extra step and might be slightly slower than the second one. =head1 IMPLEMENTATION Hold on. =head1 MIGRATION This introduces new functionality that does not break any existing code. No translation should be required. =head1 REFERENCES RFC 14: Modify open() to support FileObjects and Extensibility RFC 168: Built-in functions should be functions RFC 161: OO Integration/Migration Path http://www.mail-archive.com/perl6-language%40perl.org/msg03186.html http://www.mail-archive.com/perl6-language-objects%40perl.org/msg00097.html
