Sub::Ha...ry::Enum(3)

πŸ’– NAME πŸ’–

Sub::HandlesVia::HandlerLibrary::Enum β€” it's literally a whole library of enum-related methods, bestie. That's it, that's the name. πŸŽ€


✨ SYNOPSIS ✨

Okay so here's the vibe. You want your class to have enum-flavored methods, so you go:

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, if you're feeling lazy in the best way possible, there's 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 "_". iconic behavior honestly.)


🌸 DESCRIPTION 🌸

It's a library of methods for Sub::HandlesVia. Simple as that, no cap. πŸ’…


πŸ‘‘ DELEGATABLE METHODS πŸ‘‘

This lets you delegate in a way that's roughly compatible with MooseX::Enumeration and MooX::Enumeration β€” even though honestly that whole thing is basically a renamed subset of Sub::HandlesVia::HandlerLibrary::String anyway. πŸ™ˆ

πŸ¦‹ is( $value )

Returns a boolean telling you 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. Slay.

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". Same energy, different name. πŸ’œ


πŸŽ€ TYPE CONSTRAINT SHORTCUT πŸŽ€

The Enum handler library also lets you use an "enum" shortcut right in the attribute spec. Look at her go:

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

⭐ SHORTCUT CONSTANTS ⭐

This module hands you some shortcut constants for when you wanna indicate a whole list of delegations. Very main character of it.

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

And bestie, you can combine any of these shortcuts 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.


πŸ’€ BUGS πŸ’€

Found a bug? Please report it, bestie, I'm begging: <https://github.com/tobyink/p5-sub-handlesvia/issues>.


🫢 SEE ALSO 🫢

Sub::HandlesVia.


🌟 AUTHOR 🌟

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.


πŸ”₯ 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.

okay and one last thing, this whole page is from perl v5.42.0, dated 2026-02-11, and it's the manual for Sub::Ha...ry::Enum(3) β€” love that for her πŸ’…βœ¨

πŸ“¦ where this page comes from