This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

use strict 'objects': a new pragma for using Java-like objects in Perl

=head1 VERSION

  Mantainer:    Marco Marongiu <[EMAIL PROTECTED]>
  Date: 28 Sep 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 336
  Version: 1
  Status: Developing
  Co-mantainer: Stefano Sanna  <[EMAIL PROTECTED]>

=head1 ABSTRACT

As noted in many other RFCs, many people in the Perl community are
asking for a better implementation of objects in Perl. D.Conway in
his RFC 188 gave us a good design for implementing private objects
and attributes in Perl. Our humble effort is to go one step forward,
slightly changing the RFC 188 requests and proposing two more unary
operators: protected and static.

=head1 DESCRIPTION

This RFC comes out from a discussion between the authors: a Perl
programmer and a Java programmer. It came out that the sleeping
hours we lost were worth an RFC, where together we proposed a
modification of objects implementation in Perl.

We choose to ask for a pragma, since we didn't like the idea of
Perl programmers forced to use the new operators that we are to propose.
As people is not forced to predeclare subroutines, variables and so on,
people will not be forced to declare an attribute as private or a
method as static. Perlers' laziness must be preserved!

=head1 IMPLEMENTATION

We propose to define a new schema to the C<strict> pragma: C<use
strict 'objects'>. Use-ing strict objects imports in the calling
package (or in all packages if declared inside main) three unary
operators: C<private>, C<protected> and C<static>, that behave as
stated below.

=head2 protected

Just take Conway's RFC 188 and do a s/private/protected/g :-)

=head2 private

Like Conway's private operator defined in RFC 188, but accessing an
hash key or a method that has been marked as private from outside the
defining package should result in a fatal error.

        use strict 'objects' ;

        package Base;
        
        sub new {
                my ($class, @data) = @_;
                bless private { data => [@data] }, $class;
        }
        
        
        package SortableBase;
        use base 'Base';
                
        sub sorted {
                my ($self) = @_;
                print sort @{ $self->{Base::data} }; # should die now!
        }

=head2 static

In our humble opinion, static doesn't make sense for
attributes... erm... hashes or hash slices in perl. Anyway, we think
it still makes sense to have static methods.

Static methods should be considered as public methods (unless
otherwise specified via the private or protected operator)  that can be
invoked on bare classes directly, without needing an object
instance. Methods not marked as such should return a fatal error if called
directly on the class. For example:


    package NoStatic ;

    sub new { bless { foo => 'bar' } }
    sub wen { print "Windows' evil nature" }


    package Static ;

    use strict 'objects' ;

    static sub new { bless { foo => 'bar' } }
    sub wen { print "Windows' evil nature" }


    package main ;

    my $nsobj = NoStatic->new() ; # ok
    $nsobj->wen() ; # ok
    NoStatic->wen() ; # ok
    
    my $stobj = Static->new() ; # ok
    $stobj->wen() ; # ok
    Static->wen() ; # dies (not declared as static)


Subclasses should be allowed to overload a static
method only with a prototyped one. Other attempts to redefine a static
method should result in a fatal error.

A static method can also be declared as protected or private, prefixing
with the desired operator:


    use strict 'objects' ;

    package MyClass ;

    private static sub _do_cleanup {
        # do something
    }

    sub cleaner {
        # do something
        MyClass->_do_cleanup ;
        # do something more
    }


Static methods would be needed when we want to have access to an
object oriented interface to functions. For example, it would be nice
if you could do something like:


    use strict 'objects' ;

    use POSIX @import_something ;

    my $obj = new MyClass @params ;
    # do something really-OO-style here
    # and you can stand OO here, too
    print POSIX->floor(3.1415926) ;

instead of


    use POSIX @import_something ;

    my $obj = new MyClass @params ;
    # do something really-OO-style here, then
    print floor(3.1415926) ;
    # Not so OO, indeed 
    
but this is just syntactic sugar :-)


=head1 BUGS

Our english is not very good. In case of errors we apologize.


=head1 REFERENCES

RFC 188: Objects : Private keys and methods


Reply via email to