Feature-Compat-Class
view release on metacpan or search on metacpan
made to load it by code equivalent to
require CLASS ();
and thus it must either already exist, or be locatable via the usual
@INC mechanisms.
An optional version check can also be supplied; it performs the
equivalent of
BaseClass->VERSION( $ver )
Note that class blocks do not implicitly enable the strict and warnings
pragmata; either when using the core feature or 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 use VERSION of
a version v5.36 or above will enable both these pragmata anyway, so
that will be sufficient.
method
method NAME { ... }
method NAME;
See also "method" in Object::Pad.
Attributes are not supported, other than the usual ones provided by
perl itself. Of these, only :lvalue is particularly useful.
Lexical methods are not supported.
field
field $NAME;
field @NAME;
field %NAME;
field $NAME = EXPR;
field $NAME :ATTRS... = EXPR;
See also "field" in Object::Pad.
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; }
Since version 0.04 fields of any type may take initialising
expressions. Initialiser blocks are not supported.
field $five = 5;
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:
:param
field $var :param;
field $var :param(name)
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 :param attribute and a defaulting expression, the
operator can also be written as //= or ||=. In this case, the
defaulting expression will be used even if the caller passed an
undefined value (for //=) or a false value (for ||=). This simplifies
many situations where 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
:reader, :reader(NAME)
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 _ 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
( run in 0.462 second using v1.01-cache-2.11-cpan-71847e10f99 )