MouseX::Types::Enum - Object-oriented, Java-like enum type declaration based on Mouse
Most simple declaration and usage is,
{ package Day; use strict; use warnings FATAL => 'all'; use MouseX::Types::Enum qw/ Sun Mon Tue Wed Thu Fri Sat /; } Day->Sun == Day->Sun; # 1 Day->Sun == Day->Mon; # '' Day->Sun->to_string; # 'APPLE' Day->enums; # { Sun => Day->Sun, Mon => Day->Mon, ... }
Advanced declaration and usage is,
{ package Fruits; use MouseX::Types::Enum ( APPLE => { name => 'Apple', color => 'red' }, ORANGE => { name => 'Cherry', color => 'red' }, BANANA => { name => 'Banana', color => 'yellow', has_seed => 0 } ); has name => (is => 'ro', isa => 'Str'); has color => (is => 'ro', isa => 'Str'); has has_seed => (is => 'ro', isa => 'Int', default => 1); sub make_sentence { my ($self, $suffix) = @_; $suffix ||= ""; return sprintf("%s is %s%s", $self->name, $self->color, $suffix); } } Fruits->APPLE == Fruits->APPLE; # 1 Fruits->APPLE == Fruits->ORANGE; # '' Fruits->APPLE->to_string; # 'APPLE' Fruits->APPLE->name; # 'Apple'; Fruits->APPLE->color; # 'red' Fruits->APPLE->has_seed; # 1 Fruits->APPLE->make_sentence('!!!'); # 'Apple is red!!!' is_deeply( Fruits->enums, { APPLE => Fruits->APPLE, ORANGE => Fruits->ORANGE, BANANA => Fruits->BANANA } );
MouseX::Types::Enum provides Java-like enum type declaration.
Enums declared are
distinguished from each other
able to have attributes
able to have methods
Copyright (C) Naoto Ikeno.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Naoto Ikeno <ikenox@gmail.com>
To install MouseX::Types::Enum, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MouseX::Types::Enum
CPAN shell
perl -MCPAN -e shell install MouseX::Types::Enum
For more information on module installation, please visit the detailed CPAN module installation guide.