perl

 view release on metacpan or  search on metacpan

pod/perlclass.pod  view on Meta::CPAN


    field $s :writer;

    # Equivalent to
    field $s;
    method set_s($new) { $s = $new; return $self; }

By default the accessor method will have the name of the field minus the
leading sigil with the string C<set_> prefixed to it, but a different name
can be specified in the attribute's value.

    field $x :writer(write_x);

    # Generates a method
    method write_x ($new) { ... }

Currently, writer accessors can only be applied to scalar fields.  Attempts
to apply this attribute to a non-scalar field will result in a fatal exception
at compile-time.  This may be relaxed in a future version to allow writers on
array or hash fields.  For now, these will have to be created manually.

=head2 Method attributes

None yet.

=head1 OBJECT LIFECYCLE

=head2 Construction

Each object begins its life with a constructor call. The constructor is always
named C<new> and is invoked like a method call on the class name:

    my $object = My::Class->new(%arguments);

During object construction, class fields are looked up in the C<%arguments>
hash and populated where possible.

=head2 Adjustment

Object adjustment is a way to run arbitrary user-defined code during object
construction. This is done by placing code in C<ADJUST> blocks. Every time an
object is constructed, its C<ADJUST> blocks are executed (in the order in which
they are declared).

    class WellAdjusted {
        field $x :param;
        ADJUST {
            say "Hello!";
        }
        ADJUST {
            say "x = $x";
        }
    }

    my $object = WellAdjusted->new(x => 42);
    # Output:
    #   Hello!
    #   x = 42

C<ADJUST> blocks are syntactically similar to L<C<BEGIN> or C<INIT>
blocks|perlmod/BEGIN, UNITCHECK, CHECK, INIT and END>, which only run once.
However, C<ADJUST> blocks, like methods, have access to C<$self> (a lexical
variable holding the object being constructed) as well as all object fields
created up to that point.

=head2 Lifetime

After the construction phase, the object is ready to be used.

Using C<blessed> (C<Scalar::Util::blessed> or C<builtin::blessed>) on the
object will return the name of the class, while C<reftype>
(C<Scalar::Util::reftype> or C<builtin::reftype>) will return the string
C<'OBJECT'>.

=head2 Destruction

An object is destroyed when the last reference to it goes away, just as with
other data structures in Perl.

=head1 TODO

This feature is still experimental and very incomplete. The following list
gives an overview of features still to be added or changed:

=over 4

=item * Roles

Some syntax for declaring a role (likely a C<role> keyword), and for consuming
a role into a class (likely a C<:does()> attribute).

=item * Parameters to ADJUST blocks

Some syntax for declaring that an C<ADJUST> block can consume named
parameters, which become part of the class constructor's API. This might be
inspired by a similar plan to add named arguments to subroutine signatures.

    class X {
        ADJUST (:$alpha, :$beta = 123) {
           ...
        }
    }

    my $obj = X->new(alpha => 456);

=item * ADJUST blocks as true blocks

Currently, every ADJUST block is wrapped in its own CV (subroutine) that gets
invoked with the full ENTERSUB overhead. It should be possible to use the same
mechanism that makes all field initializer expressions appear within the same
CV on ADJUST blocks as well, merging them all into a single CV per class. This
will make it faster to invoke if a class has more than one of them.

=item * More accessor generator attributes

Attributes to request that other kinds of accessor methods be generated for
fields. Likely C<:writer>.

    class X {
        field $name :writer;
    }



( run in 0.677 second using v1.01-cache-2.11-cpan-5a3173703d6 )