NAME
fields::guarded - fields guarded against direct external access
VERSION
version 1.000001
SYNOPSIS
{
package Foo;
use fields::guarded ('foo' => [ 'get', 'set' ], 'bar' => [ 'is' ]);
sub new {
my ($class) = @_;
my Foo $self = fields::guarded->new($class);
$self->{'foo'} = 12; # Inside the package, these statements will behave as 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_bar());
$dave->{'foo'} = 78; # Dies because any usage of the object ref as a hash ref outside the package will generate a runtime error
# subclassing
{
package Bar;
use base qw(Foo);
use fields::guarded ('baz' => []);
sub new {
my ($class) = @_;
my Bar $self = fields::guarded->new($class);
$self->SUPER::new(); # initialize the base class
$self->{'baz'} = 90;
return $self;
}
}
DESCRIPTION
The fields::guarded 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 expects a hash where keys are the field names and values are the properties of the fields. The properties are specified as array references, whose elements are strings, and currently only relates to the creation of the accessors. The allowed values are:
get: will trigger the generation of a getter namedget_<field name>for the associated fieldis: will trigger the generation of a getter namedis_<field name>for the associated field (it is a variant of the one generated withget, intended for booleans fields)set: will trigger the generation of a setter namedset_<field name>for the associated field
The generated getters are subroutines that no take parameters and return the value of the associated field while the generated setters are subroutines that take a single scalar parameter, representing the value to be assigned to the field, and do not return a value.
If the class defines subroutines whose names are the same as subroutines generated by fields::guarded, a Subroutine %s redefined warning will be issued when warnings are enabled.
The added guarding mechanism applies to 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.
This mechanism is intended to discourage direct field access and enforce encapsulation conventions. It should not be considered a security boundary against deliberately circumventing the access restrictions.
SUBROUTINES
new($class)
- parameters
- returns
- dies
Creates and blesses a hash comprised of the fields declared using the fields::guarded pragma into the specified class. It is the recommended way to construct a fields-based object.
Note: In order to reduce boilerplate in the caller, if the parameter passed to the subroutine is a blessed ref, it returns that ref early.
AUTHOR
Antoine Maréchal <a.marechal.1994@gmail.com>
BUGS
Please report any bugs or feature requests to bug-fields-guarded at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=fields-guarded. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc fields::guarded
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
GitHub issue tracker
https://github.com/antoinemarechal/perl-fields-guarded/issues
Search CPAN
LICENSE AND COPYRIGHT
This software is Copyright (c) 2026 by Antoine Maréchal <a.marechal.1994@gmail.com>.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)