Sub::HandlesVia::HandlerLibrary::Enum(3) User Contributed Perl Documentation Sub::HandlesVia::HandlerLibrary::Enum(3)

Sub::HandlesVia::HandlerLibrary::Enum - library of enum-related methods

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 "_".)

This is a library of methods for Sub::HandlesVia.

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.

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

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

An alias for "assign".

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' },
  );
}

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' },
);

Creates delegations named like "is_pass" and "is_fail".

Creates delegations named like "status_is_pass" and "status_is_fail".

Creates delegations named like "set_pass" and "set_fail".

Creates delegations named like "status_set_pass" and "status_set_fail".

Please report any bugs to https://github.com/tobyink/p5-sub-handlesvia/issues.

Sub::HandlesVia.

Toby Inkster <tobyink@cpan.org>.

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.

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.

2024-09-02 perl v5.40.0