New module aimed at being a 'fields' extension
[email protected] (Antoine Maréchal)
| Newsgroups | perl.module-authors |
|---|---|
| Message-ID | <CAHZ=n8XeXfYKEqkNHSVrSfhFdPm-soWwyt6q0wkmswJFxY6BBw@mail.gmail.com> |
Hello everyone,
I would like to upload a new module to CPAN that is an "extension" of the
'fields' pragma. The goal of the module is to provide the same features as
fields with two added features: a guard to discourage usage of the object
ref as hash ref and a generator of basic accessors (see Synopsis and
Description below for details).
With this email, I am searching for guidance on the naming of the module.
As you can see below, it is for now jokingly called 'fields::plusplus'
which is probably not fitting. Although 'fields::guarded' might be a more
serious attempt, is using the 'fields::' namespace allowed at all ? If not,
would the 'Class::' namespace be a good alternative (like:
Class::GuardedFields) ?
Thank you in advance for your time and any suggestion and/or feedback,
Antoine Maréchal
-----
SYNOPSIS
{
package Foo;
use fields::plusplus ('foo' => [ 'get', 'set' ], 'bar' => [ 'is' ]);
sub new {
my ($class) = @_;
my Foo $self = fields::plusplus->new($class);
$self->{'foo'} = 12; # Inside the package, those statements
will behave like they would with fields,
$self->{'bar'} = 1; # meaning that the class fields will be
compile-time and run-time checked
$self->{'oof'} = 34; # That statement will thus generate a
compile-time error
return $self;
}
}
my Foo $dave = Foo->new();
$dave->set_foo(56); # Setter automatically generated based on the
import arguments
print($dave->get_foo()); # Getters automatically generated based on
the import arguments
print($dave->is_foo());
$dave->{'foo'} = 78; # Outside the package, any usage of the object
ref as a hash ref will generate a run-time error
# subclassing
{
package Bar;
use base qw(Foo);
use fields::plusplus ('baz' => []);
sub new {
my ($class) = @_;
my Bar $self = fields::plusplus->new($class);
$self->SUPER::new();
$self->{'baz'} = 90;
return $self;
}
}
DESCRIPTION
The fields::plusplus pragma builds upon the fields pragma and adds:
-
a guarding mechanism discouraging the use of the object as a hash ref
-
the generation of accessors based on import arguments
On import, this pragma is expecting a hash where keys are the field names
and values are the properties of the fields. For now, the properties are
only related to the creation of the accessors. The allowed values are:
-
get: will trigger the generation of a getter named get_<field name> for
the associated field
-
is: will trigger the generation of a getter named is_<field name> for
the associated field (it is a variation of the one generated with get
intended for booleans)
-
set: will trigger the generation of a setter named set_<field name> for
the associated field
The added guarding mechanism applies on object refs created through the
"new" subroutine. It checks at runtime that the object ref is not used as a
hash ref outside the package and dies otherwise. Inside the package, it
does not restrict the usage of the object ref and lets it benefit from the
features of fields.