Sub::HandlesVia::HandlerLibrary::Enum - library of enum-related methods
Contents
Bugs
Please report any bugs to <https://github.com/tobyink/p5-sub-handlesvia/issues>.
Copyright And Licence
This software is copyright (c) 2022 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5
programming language system itself.
Delegatable Methods
This allows for delegation roughly compatible with MooseX::Enumeration and MooX::Enumeration, even though
that's basically a renamed subset of Sub::HandlesVia::HandlerLibrary::String anyway.
is($value)
Returns a boolean indicating whether the enum is that value.
my $object = My::Class->new( status => 'pass' );
say $object->is_pass(); ## ==> true
say $object->is_fail(); ## ==> false
assign($value)
Sets the enum to the value.
my $object = My::Class->new( status => 'pass' );
say $object->is_pass(); ## ==> true
say $object->is_fail(); ## ==> false
$object->assign_fail();
say $object->is_pass(); ## ==> false
say $object->is_fail(); ## ==> true
set($value)
An alias for "assign".
Description
This is a library of methods for Sub::HandlesVia.
Disclaimer Of Warranties
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
perl v5.40.1 2025-04-01 Sub::HandlesVi...erLibrary::Enum(3pm)
Name
Sub::HandlesVia::HandlerLibrary::Enum - library of enum-related methods
See Also
Sub::HandlesVia.
Shortcut Constants
This module provides some shortcut constants for indicating a list of delegations.
package My::Class {
use Moo;
use Types::Standard qw( Enum );
use Sub::HandlesVia;
use Sub::HandlesVia::HandlerLibrary::Enum qw( HandleIs );
has status => (
is => 'ro',
isa => Enum[ 'pass', 'fail' ],
handles_via => 'Enum',
handles => HandleIs,
default => sub { 'fail' },
);
}
Any of these shortcuts can be combined using the " | " operator.
has status => (
is => 'ro',
isa => Enum[ 'pass', 'fail' ],
handles_via => 'Enum',
handles => HandleIs | HandleSet,
default => sub { 'fail' },
);
"HandleIs"
Creates delegations named like "is_pass" and "is_fail".
"HandleNamedIs"
Creates delegations named like "status_is_pass" and "status_is_fail".
"HandleSet"
Creates delegations named like "set_pass" and "set_fail".
"HandleNamedSet"
Creates delegations named like "status_set_pass" and "status_set_fail".
Synopsis
package My::Class {
use Moo;
use Sub::HandlesVia;
use Types::Standard 'Enum';
has status => (
is => 'ro',
isa => Enum[ 'pass', 'fail' ],
handles_via => 'Enum',
handles => {
'is_pass' => [ is => 'pass' ],
'is_fail' => [ is => 'fail' ],
'assign_pass' => [ assign => 'pass' ],
'assign_fail' => [ assign => 'fail' ],
},
default => sub { 'fail' },
);
}
Or, using a shortcut:
package My::Class {
use Moo;
use Sub::HandlesVia;
use Types::Standard 'Enum';
has status => (
is => 'ro',
isa => Enum[ 'pass', 'fail' ],
handles_via => 'Enum',
handles => {
'is_pass' => 'is_pass',
'is_fail' => 'is_fail',
'assign_pass' => 'assign_pass',
'assign_fail' => 'assign_fail',
},
default => sub { 'fail' },
);
}
(Sub::HandlesVia::HandlerLibrary::Enum will split on "_".)
Type Constraint Shortcut
The Enum handler library also allows an "enum" shortcut in the attribute spec.
package My::Class {
use Moo;
use Sub::HandlesVia;
has status => (
is => 'ro',
enum => [ 'pass', 'fail' ],
handles_via => 'Enum',
handles => {
'is_pass' => [ is => 'pass' ],
'is_fail' => [ is => 'fail' ],
'assign_pass' => [ assign => 'pass' ],
'assign_fail' => [ assign => 'fail' ],
},
default => sub { 'fail' },
);
}
