Feature-Compat-Class

 view release on metacpan or  search on metacpan

lib/Feature/Compat/Class.pm  view on Meta::CPAN

If the package providing the superclass does not exist, an attempt is made to
load it by code equivalent to

   require CLASS ();

and thus it must either already exist, or be locatable via the usual C<@INC>
mechanisms.

An optional version check can also be supplied; it performs the equivalent of

   BaseClass->VERSION( $ver )

Note that C<class> blocks B<do not> implicitly enable the C<strict> and
C<warnings> pragmata; either when using the core feature or C<Object::Pad>.
This is to avoid surprises when eventually switching to purely using the core
perl feature, which will not do that. Remember however that a C<use VERSION>
of a version C<v5.36> or above will enable both these pragmata anyway, so that
will be sufficient.

=head2 method

   method NAME { ... }
   method NAME;

See also L<Object::Pad/method>.

Attributes are not supported, other than the usual ones provided by perl
itself. Of these, only C<:lvalue> is particularly useful.

Lexical methods are not supported.

=head2 field

   field $NAME;
   field @NAME;
   field %NAME;

   field $NAME = EXPR;

   field $NAME :ATTRS... = EXPR;

See also L<Object::Pad/field>.

Most field attributes are not supported. In particular, rather than using the
accessor-generator attributes you will have to create accessor methods
yourself; such as

   field $var;
   method var { return $var; }
   method set_var ($new_var) { $var = $new_var; }

I<Since version 0.04> fields of any type may take initialising expressions.
Initialiser blocks are not supported.

   field $five = 5;

I<Since version 0.07> field initialiser expressions can see earlier fields
that have already been declared, and use their values:

   field $fullname  :param;
   field $shortname :param = ( split m/ +/, $fullname )[0];

The following field attributes are supported:

=head3 :param

   field $var :param;

   field $var :param(name)

I<Since version 0.04.>

Declares that the constructor will take a named parameter to set the value for
this field in a new instance.

   field $var :param = EXPR;

Without a defaulting expression, the parameter is mandatory. When combined
with a defaulting expression, the parameter is optional and the default will
only apply if the named parameter was not passed to the constructor.

   field $var :param //= EXPR;
   field $var :param ||= EXPR;

With both the C<:param> attribute and a defaulting expression, the operator
can also be written as C<//=> or C<||=>. In this case, the defaulting
expression will be used even if the caller passed an undefined value (for
C<//=>) or a false value (for C<||=>). This simplifies many situations where
C<undef> would not be a valid value for a field parameter.

   class C {
      field $timeout :param //= 20;
   }

   C->new( timeout => $args{timeout} );
   # default applies if %args has no 'timeout' key, or if its value is undef

=head3 :reader, :reader(NAME)

I<Since version 0.07.>

Generates a reader method to return the current value of the field. If no name
is given, the name of the field is used. A single prefix character C<_> will
be removed if present.

   field $x :reader;

   # equivalent to
   field $x;  method x () { return $x }

These are permitted on an field type, not just scalars. The reader method
behaves identically to how a lexical variable would behave in the same
context; namely returning a list of values from an array or key/value pairs
from a hash when in list context, or the number of items or keys when in
scalar context.

   field @items :reader;

   foreach my $item ( $obj->items ) { ... }   # iterates the list of items

   my $count = $obj->items;                   # yields count of items



( run in 1.011 second using v1.01-cache-2.11-cpan-71847e10f99 )