NAME

Object::Enum - replacement for if ($foo eq 'bar')

SYNOPSIS

use Object::Enum qw(Enum);

my $color = Enum([ qw(red yellow green) ]);
# ... later
if ($color->is_red) {
# it can't be yellow or green

EXPORTS

See Sub::Exporter for ways to customize this module's exports.

Enum

An optional shortcut for Object::Enum->new.

CLASS METHODS

new

my $obj = Object::Enum->new(\@values);
# or
$obj = Object::Enum->new(\%arg);

Return a new Object::Enum, with one or more sets of possible values.

The simplest case is to pass an arrayref, which returns an object capable of having any one of the given values or of being unset.

The more complex cases involve passing a hashref, which may have the following keys:

  • unset

    whether this object can be 'unset' (defaults to true)

  • default

    this object's default value is (defaults to undef)

  • values

    an arrayref, listing the object's possible values (at least one required)

  • readonly

    boolean value to indicate if the object is read-only. If set to read-only the objects value and set_* methods become ineffectual.

OBJECT METHODS

spawn

clone

my $new = $obj->clone;

my $new = $obj->clone($value);

Create a new Enum from an existing object, using the same arguments as were originally passed to new when that object was created.

An optional value may be passed in; this is identical to (but more convenient than) calling value with the same argument on the newly cloned object.

This method was formerly named spawn. That name will still work but is deprecated.

readonly

my $obj = $obj->readonly(\@values, $value)

Creates a read-only enum object, also known as immutable. When enum objects are created with this set_* methods, and the value method will become ineffectual.

If you want a mutable version, simply clone the immutable version

my $new_obj = $readonly_obj->clone;
$new_obj->set_red;

value

The current value as a string (or undef)

Note: don't pass in undef; use the unset method instead.

values

The possible values for this object

unset

Unset the object's value (set to undef)

is_*

set_*

Automatically generated from the values passed into new.

None of these methods take any arguments.

The set_* methods are chainable; that is, they return the object on which they were called. This lets you do useful things like:

use Object::Enum Enum => { -as => 'color', values => [qw(red blue)] };

print color->set_red->value; # prints 'red'

AUTHOR

Hans Dieter Pearcey, <hdp at cpan.org>

BUGS

Please report any bugs or feature requests to bug-object-enum at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Enum. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

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

perldoc Object::Enum

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2006 Hans Dieter Pearcey, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.