The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


accessors::rw::explicit - RW object attribute accessors, with explicit semantics


Version 0.01


    package Foo;
    use accessors::rw::explicit qw(foo bar baz);

    my $obj = bless {}, 'Foo';

    # always return the current value, even on set:
    $obj->set_foo( 'hello ' ) if $obj->bar( 'world' ) eq 'world';

    print $obj->foo, $obj->bar, $obj->set_baz( "!\n" );


ceci n'est pas un Moose

The purpose of this module is to provide very basic object instance attribute accessors for basic perl5 blessed hash-reference objects.

That is a mouthful. Essentially is is just an exercise in codifying convention, and sensible code reuse. It is not an attempt at a complete object system, with flexible attribute definitions. If you want that, you want Moose, end of.

This module will set up attribute accessors for you though, and allow you to specify a prefix on your getters and setters. It denotes itself as explicit, as it defaults to different names for the getters and setters, namely prepending "set_" to setters.

so for foo you would get:

  $obj->set_foo( $foo );


  my $foo = $obj->foo;

These prefixes can be changed by providing options on import.


  use accessors::rw::explicit ({get_prefix => 'you_tell_me_', set_prefix => 'I_tell_you_'}, 'foo');

would provide:

  $obj->I_tell_you_foo( $foo )


  my $foo = $obj->you_tell_me_foo();



returns the prefix prepended to getters. Defaults to the empty string.


returns the prefix prepended to setters. Defaults to "set_".


As with other accessors modules, this takes a list of the attributes you want setting up:

  use accessors::rw::explicit qw(foo bar baz);

It can also change the default get and set prefixes by providing an optional options hash:

  use accessors::rw::explicit ({get_prefix => 'get_'}, qw/foo bar baz/);

The above would produce accessors that conform to the Perl Best Practice book's recommendations.


Creates a get accessor of the form GetPrefix + AttributeName and a set accessor of the form SetPrefix + AttributeName

See import for how to define the prefixes and the attribute names.

This overrides a method in the package, and should never need to be called directly.


The routine that actually creates the accessors. The body of a getter looks like:

  my $getter = sub {
    return $_[0]->{$property};

and a setter is defined as:

  my $setter = sub {
    $_[0]->{$property} = $_[1];
    return $_[0]->{$property};

Where $property is defined to be

  "-" . $attribute_name</code>. 


Alex Kalderimis, <alex dot kalderimis at gmail dot com>


Please report any bugs or feature requests to bug-accessors-rw-explicit at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc accessors::rw::explicit

You can also look for information at:


Steve Purkis for writing


Copyright 2011 Alex Kalderimis.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.